TypeScript extends JavaScript by adding compile-time types, interfaces, and generics. Understand how the `tsc` compiler type-checks, strips types, and outputs standard JavaScript.
Contains static types, interfaces, generics, and annotations.
Type-checks the AST against tsconfig rules and reports errors.
All types, interfaces, and annotations are stripped completely.
Browser V8 / Node.js executes standard vanilla JavaScript.
Catches type mismatches, null pointer bugs, and invalid object access at compile time rather than crashing in production.
// Type inference
let count = 42; // inferred as number
// count = "forty-two"; // Error: Type 'string' is not assignable to type 'number'Create flexible, reusable components that work over a variety of types while maintaining strict type safety.
function getFirstItem<T>(items: T[]): T | undefined {
return items[0];
}
const num = getFirstItem([10, 20, 30]); // typed as numberPattern matching over variant objects using a shared literal tag field to guarantee exhaustive case handling.
type State =
| { status: "loading" }
| { status: "success"; data: string[] }
| { status: "error"; error: Error };Built-in type transformers to manipulate existing interfaces without code duplication.
type User = { id: string; name: string; email: string };
type UpdateUserDto = Partial<User>; // all optional
type PublicUser = Omit<User, "email">; // omit email