TypeScript 7 · Utility types and narrowing
Narrow an unknown value
Check the type before using a value of type unknown, such as parsed JSON.
function parse(input: unknown) {
if (typeof input === "object" && input !== null && "id" in input) {
return input.id;
}
}
Official TypeScript 7 documentation
More utility types and narrowing commands
- Make all properties optional
- Pick or omit properties
- Type a dictionary
- Require or freeze properties
- Remove null and undefined
- Write a type guard
All utility types and narrowing commands · TypeScript 7 cheatsheet