TypeScript
TypeScript is JavaScript with a static type system added on top.
What Is TypeScript
TypeScript helps catch mistakes before code runs by checking the shapes of values, function inputs, return values, and objects.
It compiles to JavaScript, so it can run anywhere JavaScript runs.
How To Use TypeScript
Install TypeScript, write .ts or .tsx files, and compile or bundle them into JavaScript.
Many modern frameworks include TypeScript support out of the box.
Basic Example
type User = {
id: number;
name: string;
};
function formatUser(user: User): string {
return `${user.id}: ${user.name}`;
}
console.log(formatUser({id: 1, name: "Tiew"}));
Common Concepts
- Types describe values.
- Interfaces and type aliases model objects.
- Generics make reusable typed utilities.
- Type narrowing refines possible values.
What To Learn Next
Learn union types, generics, utility types, typed React components, and how to configure tsconfig.json.