typeof
A keyword that asks for the type of a value, both in JavaScript at run time and in TypeScript types.
In more detail
In JavaScript, typeof x returns a string such as "number". In a TypeScript type position, typeof router means “the type of this value”, which is how export type AppRouter = typeof router shares a server’s shape without sharing its code.
Examples
if (typeof param === "number") return param.toFixed(0);type Environment = typeof API_CONFIG["environment"];Related terms
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- tRPC router — The object that groups a server’s tRPC procedures by name; its type, usually exported as AppRouter, is all the client needs.
- type narrowing — The compiler refining a broad type to a more specific one inside a block, after a check proves which case applies.
- type guard — A run time check, such as typeof x === "string" or a custom function, that the compiler uses to narrow a type.
- as const — The TypeScript suffix as const, which tells the compiler to treat an object's values as fixed literal values rather than general types.
- null — A special value meaning no value or no object, set on purpose to show that something is empty or absent.
Taught in
- End-to-End Type Safety: Shared Schemas & DTOs — Production Full-Stack Engineering
- Environment Configuration: Secrets & Multi-Stage — Production Full-Stack Engineering
- Type-Safe RPC Architectures: tRPC & Procedures — Production Full-Stack Engineering
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- any Versus unknown: Defensive Typing In Practice — Production TypeScript
- Typeof Guards, Truthiness And Equality Narrowing — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Union Utilities: Exclude, Extract, NonNullable And Awaited — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript