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.