Allow multiple possible primitive types.
Section: Primitive Types and Annotations
Use a union type
typescript
typescript
let value: string | number;
value = "ok";
value = 42;Explanation
Union types are one of the most common TypeScript patterns.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Primitive Types and Annotations
Restrict values with literal unions
Use exact string values for safer APIs.
Annotate basic variables
Explicitly type strings, numbers, and booleans.
Annotate object shapes
Describe the expected properties of an object.
Freeze literals with `as const`
Preserve readonly literal values for objects and arrays.