TypeScript
Typed JavaScript.
When To Use
- Type safety needed
- Large codebases
- Modern JS projects
What To Do
1. Types
interface User {
id: number
name: string
email?: string // optional
}
function getUser(id: number): User {
return { id, name: 'User' }
}
2. Generics
function first<T>(arr: T[]): T {
return arr[0]
}
3. Compile
npx tsc --init
npx tsc
npx tsc --watch
4. Config
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true
}
}
Role: TypeScript Developer
Input: .ts files
Output: .js files
Typed JS.