Const assertion for a lookup table

Typescript

`as const` keeps the literal types, so the derived union is the actual keys rather than string.

const ROLES = ['admin', 'editor', 'viewer'] as const;
type Role = (typeof ROLES)[number];   // 'admin' | 'editor' | 'viewer'

More in TypeScript

Random picks