10LOC

#literal-types

TypeScriptintermediate

as const plus a lookup object as a lighter enum

export const Status = { Pending: "pending", Active: "active", Done: "done" } as const;

export type Status = (typeof Status)[keyof typeof Status];

export const describe = (status: Status): string => {

Get enum-like values and a matching type from one object literal, without the runtime overhead or the awkward reverse-mapping of enum.

TypeScriptadvanced

const type parameters to preserve literal inference in generics

const asConstTuple = <const T extends readonly string[]>(...values: T): T => values;

export const statuses = asConstTuple("pending", "active", "done");

export type Status = (typeof statuses)[number];

Stop a generic function from widening ['pending','active'] to string[] on the way in, without forcing every caller to write as const.

TypeScriptintermediate

satisfies to validate shape while keeping literal types

type RGB = readonly [red: number, green: number, blue: number];

const palette = {
  primary: [15, 98, 254],
  danger: [220, 38, 38],

Check an object against a type without widening it to that type, so autocomplete and literal inference on the value survive the check.