当前位置: 首页 > ds >正文

【TypeScript】索引签名类型(Index Signatures)

索引签名类型

索引签名(Index Signatures)允许定义对象中动态属性名的类型规则,特别适合处理键值对集合或不确定属性名的对象结构。

🏷️ 基本语法

interface Dictionary {[key: string]: number; // 字符串索引签名
}interface NumericDictionary {[index: number]: string; // 数字索引签名
}

🌟 核心特性

1. 字符串索引签名

interface StringKeyObject {[key: string]: any; // 允许任意字符串作为键name: string;       // ✅ 可以包含已知属性(必须符合索引签名约束)
}const obj: StringKeyObject = {name: "Alice",age: 30,            // ✅ 符合 [key: string]: any"favorite-color": "blue"
};

2. 数字索引签名

interface NumberKeyArray {[index: number]: string; // 类似数组的结构length: number;          // 可以包含其他属性
}const arr: NumberKeyArray = {0: "first",1: "second",length: 2
};

3. 混合索引签名

interface HybridDictionary {[key: string]: string | number;[index: number]: string;  // 数字索引返回值必须是字符串索引的子类型id: number;              // 已知属性必须符合索引签名
}

🛠️ 实际应用场景

1. 动态属性对象

interface DynamicConfig {[setting: string]: boolean | string;debugMode: boolean; // 特定已知属性
}const config: DynamicConfig = {debugMode: true,"feature.autoSave": true,"ui.theme": "dark"
};

2. 类型安全的字典

interface WordDictionary {[word: string]: {definition: string;synonyms: string[];};
}const dictionary: WordDictionary = {"ephemeral": {definition: "lasting for a very short time",synonyms: ["transient", "fleeting"]}
};

3. 模拟数组结构

interface MyArray<T> {[n: number]: T;length: number;push(...items: T[]): number;
}const nums: MyArray<number> = {0: 1,1: 2,length: 2,push: function(...items) { /* 实现 */ }
};

⚠️ 重要限制与注意事项

  1. 类型一致性规则

    interface Problematic {[key: string]: number;name: string; // ❌ 错误:string 不符合 number
    }
    
  2. 只读索引签名

    interface ReadonlyMap {readonly [key: string]: number;
    }const map: ReadonlyMap = { count: 1 };
    // map.count = 2; ❌ 错误:只读属性
    
  3. 与 Record 工具类型的区别

    // 索引签名方式
    interface StringToNumber {[key: string]: number;
    }// Record 方式
    type StringToNumber = Record<string, number>;/* 区别:- Record 更简洁- 索引签名可以在接口中与其他属性共存
    */
    

🚀 高级技巧

1. 模板字面量索引签名(TS 4.4+)

interface Headers {[header: `x-${string}`]: string;"content-type": string;
}const headers: Headers = {"x-request-id": "123","x-api-key": "secret","content-type": "application/json"// "auth-token": "xxx" ❌ 不符合模板
};

2. 条件索引签名

type Conditional<T> = T extends "readonly" ? { readonly [key: string]: number } : { [key: string]: number };const readOnlyDict: Conditional<"readonly"> = { count: 1 };
// readOnlyDict.count = 2; ❌ 错误

索引签名是 TypeScript 灵活类型系统的关键特性之一,合理使用可以:

  • 处理动态数据结构 🔄
  • 保持类型安全 🛡️
  • 减少 any 的使用 🚫
  • 提高代码可维护性 🧹
http://www.xdnf.cn/news/5389.html

相关文章:

  • 字符串---StringBuilder的使用
  • Kubernetes生产实战(一):多容器Pod协同实践
  • 超详细Kokoro-82M本地部署教程
  • JavaScript基础-switch分支流程控制
  • 3498. 字符串的反转度
  • MATLAB安装常见问题及解决方案详解(含代码示例)
  • 抖音app 抓包分析
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(18):条件形 文法
  • AI编程: 使用Trae1小时做成的音视频工具,提取音频并识别文本
  • 【python】json解析:invalid literal for int() with base 10: ‘\“\“‘“
  • 模型 启动效应
  • python如何提取Chrome中的保存的网站登录用户名密码?
  • 【日撸 Java 三百行】综合任务 1
  • Spark流水线在线演示
  • 小程序初始化加载时间优化 步骤思考与总结
  • (二)Linux下基本指令 2
  • 碰一碰发视频源码搭建的定制化开发指南,支持OEM
  • Vue v-model 深度解析:实现原理与高级用法
  • 【c++】多态详解
  • 【MySQL】数据表插入数据
  • 基于python的少儿兴趣班推荐系统的设计与实现
  • 微服务6大拆分原则
  • C++修炼:stack和queue
  • 服务器综合实验(实战详解)
  • 【新教程】Linux服务器ssh启用两步验证
  • 什么是深拷贝什么是浅拷贝,两者区别
  • PPO近端策略优化算法
  • Tailwind CSS v4 主题化实践入门(自定义 Theme + 主题模式切换)✨
  • shell 编程之正则表达式与文本处理器
  • JS逆向-7881游戏平台Lb-Sign参数和Lb-Timestamp参数