Dictionary type

2 snippets in TypeScript

TSTypeScript

Index Signatures

TS · Interfaces
Syntax
interface Name {
  [key: string]: Type;
  [index: number]: Type;
}
Example
interface TranslationMap {
  [locale: string]: string;
}

const greetings: TranslationMap = {
  en: "Hello",
  es: "Hola",
  ja: "こんにちは",
};

// Mixed: known + dynamic keys
interface AppConfig {
  appName: string;
  version: string;
  [key: string]: string; // all values must be string
}
Output
// greetings["fr"] is string (even if missing at runtime)

Note Index signatures allow any key of the given type. When combined with named properties, all named property types must be compatible with the index signature type. Enable noUncheckedIndexedAccess in tsconfig to get T | undefined instead of just T for indexed access - much safer.

Record<Keys, Value>

TS · Utility Types
Syntax
type Result = Record<KeyType, ValueType>;
Example
type Role = "admin" | "editor" | "viewer";

interface Permission {
  canRead: boolean;
  canWrite: boolean;
  canDelete: boolean;
}

const rolePermissions: Record<Role, Permission> = {
  admin:  { canRead: true,  canWrite: true,  canDelete: true },
  editor: { canRead: true,  canWrite: true,  canDelete: false },
  viewer: { canRead: true,  canWrite: false, canDelete: false },
};
Output
// Record forces every Role to have a Permission entry

Note When Keys is a union of string literals, Record ensures every literal is present - great for exhaustive mappings. Record<string, T> is equivalent to { [key: string]: T } and does not enforce specific keys.

Frequently asked questions

How does TypeScript handle dictionary type?
TypeScript covers this with 2 copy-ready snippets on this page. The "Index Signatures" snippet in TypeScript uses `interface Name {`.
Which code does the TypeScript example use?
The "Index Signatures" snippet uses `interface Name {`, from the Interfaces section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "dictionary type"?
Besides "Index Signatures", this page also shows "Record<Keys, Value>".
Is there anything to watch out for?
Yes. For "Index Signatures": Index signatures allow any key of the given type. When combined with named properties, all named property types must be compatible with the index signature type. Enable noUncheckedIndexedAccess in tsconfig to get T | undefined instead of just T for indexed access - much safer.