skip to content
Alvin Lucillo

Casting types in Typescript

/ 1 min read

💻 Tech

Casting types in TypeScript supports strong typing, especially when you have literal values you want to cast to a particular type.

interface Person {
	name: string;
	age: number;
}

// using as keyword
const p1: Person = { name: 'Fizz', age: 21 } as Person;

// using angle bracket
const p2 = <Person>{ name: 'Fizz', age: 21 };