Core types
suggest changeTypeScript understands JavaScript's primitive types—string, number, bigint, boolean, symbol, null, and undefined—and adds ways to combine and refine them, including tuples, unions, intersections, and literal types. It also provides special types such as unknown, never, and void. Prefer unknown to any when a value must be checked before use.
type Identifier = string | number;
type Point = readonly [x: number, y: number];
function display(value: unknown): string {
if (typeof value === "string") return value;
if (typeof value === "number") return value.toString();
return "unsupported";
}
const coordinateOrigin: Point = [0, 0];
const id: Identifier = "item-42";
console.log(coordinateOrigin, display(id));
any largely disables checking and should be limited to narrow compatibility boundaries. never represents a value that cannot occur and is useful for exhaustive checks. The lowercase object type means any non-primitive value; {} and Object are broader and are rarely the right choice for an API.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents