๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ“˜ TypeScript

Index signatures

๐Ÿ“š What are Index Signatures in TypeScript? When you need an object that can have any number of string keys all mapping to the same value type -- like a dictionary -- you use an index signature: { [key: string]: number }. This says: 'this object can have any string as a key, and every value will beโ€ฆ

8 min 10 XP Lesson 14 of 21
Index signatures
๐ŸŒ

Appy Saysโ€ฆ

Sometimes you don't know the exact property names ahead of time โ€” you just know they'll all be strings mapping to numbers, or strings mapping to strings. Index signatures let you type these dynamic objects.

๐Ÿ“–

What are Index Signatures?

An index signature describes objects with dynamic keys โ€” you specify the key type and value type, without listing every property name.

  • โ€ขSyntax: { [key: string]: ValueType }
  • โ€ขinterface StringMap { [key: string]: string; }
  • โ€ขconst headers: StringMap = { 'Content-Type': 'application/json' };
  • โ€ขIndex signature means ANY string key maps to that type
  • โ€ขCan combine with specific properties: { id: number; [key: string]: string | number; }
  • โ€ขRecord type (cleaner alternative): Record<string, string> โ€” same as index signature
๐ŸŽฎ

Think of it like a Minecraft chest that holds only diamonds

You don't know what's in every slot โ€” slots are dynamic. But you know every slot either has diamonds or is empty. An index signature types this: 'I don't know the keys, but every value is a diamond (the declared type)'.

โš™๏ธ

How It Works

  • โ€ข1. interface Config { [key: string]: string | number; }
  • โ€ข2. const c: Config = { host: 'localhost', port: 3000 };
  • โ€ข3. TypeScript allows any string key, but value must be string | number
  • โ€ข4. Accessing: c['host'] โ†’ string | number
  • โ€ข5. Record shorthand: type Config = Record<string, string | number>;
  • โ€ข6. Number keys: { [index: number]: string } โ€” like arrays
๐ŸŒ

Real-World Examples

  • โ€ขHTTP headers: Record<string, string>
  • โ€ขTranslation map: Record<string, string> โ€” key=locale ID, value=translated text
  • โ€ขScore board: Record<string, number> โ€” key=player name, value=score
  • โ€ขCSS variables: Record<string, string> โ€” key=var name, value=colour
๐Ÿ’ก

Key Facts

  • โ€ขRecord<K, V> is shorthand for an index signature: { [key in K]: V }
  • โ€ขIndex signatures allow all string keys โ€” TypeScript can't narrow a specific key's type
  • โ€ขFor known keys, use a proper interface or object type literal instead
  • โ€ขWhen you access a Record value TypeScript returns T | undefined with noUncheckedIndexedAccess โ€” use that config option for safer access
โš ๏ธ

Watch Out!

Index signatures are looser than typed interfaces โ€” TypeScript allows any string key so you lose specific property guarantees. If you know all the keys, use a proper typed interface: { name: string; age: number; } is safer than Record<string, string | number>.

๐Ÿ“Œ

Remember

{ [key: string]: ValueType } or Record<string, ValueType> โ€” types objects with dynamic keys. Use when you genuinely don't know the keys ahead of time. Prefer explicit interfaces when keys are known.

โœ…

What You Learned

  • โ€ขIndex signatures type dynamic-key objects: { [key: string]: Type } or Record
  • โ€ขAny string key is allowed; value type is enforced
  • โ€ขUnlocks: HTTP headers, score maps, translation dictionaries, any key-value store

Key Facts

  • โ†’Record<K, V> is shorthand for an index signature: { [key in K]: V }
  • โ†’Index signatures allow all string keys โ€” TypeScript can't narrow a specific key's type
  • โ†’For known keys, use a proper interface or object type literal instead
  • โ†’When you access a Record value TypeScript returns T | undefined with noUncheckedIndexedAccess โ€” use that config option for safer access

Real-World Examples

โ€ข HTTP headers: <code>Record&lt;string, string&gt;</code> โ€ข Translation map: <code>Record&lt;string, string&gt;</code> โ€” key=locale ID, value=translated text โ€ข Score board: <code>Record&lt;string, number&gt;</code> โ€” key=player name, value=score โ€ข CSS variables: <code>Record&lt;string, string&gt;</code> โ€” key=var name, value=colour

Remember

{ [key: string]: ValueType } or Record<string, ValueType> โ€” types objects with dynamic keys. Use when you genuinely don't know the keys ahead of time. Prefer explicit interfaces when keys are known.

Quick Quiz

1 / 2

[key: string]: number means?