Loading...
Loading...
TypeScript catches bugs at compile time, improves IDE intellisense, and makes large codebases dramatically easier to maintain. It compiles down to plain JavaScript, so you can use it anywhere.
const name: string = 'Sabir'
const age: number = 25
const isActive: boolean = true
const skills: string[] = ['TypeScript', 'React', 'Node.js']Use interface for object shapes and type for unions, intersections, or primitives:
interface User {
id: number
name: string
email: string
role: 'admin' | 'editor' | 'reader'
}
type ID = string | number
type Nullable<T> = T | nullGenerics let you write reusable, type-safe utilities:
function first<T>(arr: T[]): T | undefined {
return arr[0]
}
const num = first([1, 2, 3]) // number | undefined
const str = first(['a', 'b', 'c']) // string | undefinedTypeScript ships with powerful built-in utility types:
interface Post {
id: number
title: string
content: string
published: boolean
}
type PostPreview = Pick<Post, 'id' | 'title'>
type DraftPost = Omit<Post, 'id'>
type PartialPost = Partial<Post>
type RequiredPost = Required<Post>A pattern for type-safe state management:
type ApiState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; message: string }TypeScript pays off massively at scale. Start with strict: true in your tsconfig.json and let the compiler guide you toward safer, cleaner code.