[key: string]: T Index Signatures

Last updated: 2026-10-22

[key: string]: T Index Signatures

Index signatures describe objects with arbitrary keys but a uniform value type — dictionaries, counters, config maps. Written as [key: string]: T.

GroupType Composition
KindSyntax
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

const dict: { [key: string]: number } = {};
dict["a"] = 1;
// Equivalent utility-type spelling
const same: Record<string, number> = {};

⚙️ Parameters

[key: string]Key type must be string, number, symbol or a union; the key name is arbitrary
[i: number][i: number]: T describes index-based access; the number index type must be a subtype of the string index type

Returns:None — index signatures are type-level only and produce no runtime code.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

180 0

(Runtime output after transpilation; scores["Bob"] types as number without error — index signatures declare every key exists)

💡 Notes

ℹ️ Info Record<string, number> means the same as { [key: string]: number }; the utility form is more common.
⚠️ Note Index signatures make every key legal: scores["Bob"] passes even under strict, so typos slip through. For a finite key set use Record<"a" | "b", T> or mapped types.
💡 Tip For mixed values use a union — { [key: string]: string | number } — and narrow after reading.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Index signature vs Record<K, V>?
A Equivalent in power — Record is an index signature implemented via mapped types. Record is terser and can also pin an exact key union.
Q Why does accessing a missing key not error?
A That is the semantics: every string key exists with type T. To catch typos, pin keys with Record<"a" | "b", T> or enable noUncheckedIndexedAccess.
Q Can number and string index signatures coexist?
A Yes, but the number index value type must be a subtype of the string one (JS numeric keys become string keys under the hood).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏