TypeScript Built-in Types
Understanding TypeScript's core type system
🎯 What are Built-in Types?
TypeScript provides built-in types for common data structures. These primitive and utility types help you write type-safe code with proper validation and autocomplete support throughout your application.
// Basic built-in types
let name: string = 'John'
let age: number = 25
let isActive: boolean = true
let items: string[] = ['a', 'b', 'c']
Primitive Types
string
Text values
let name: string
name = 'Alice'
name = "Bob"
number
Numeric values
let age: number = 25
let price: number = 99.99
let hex: number = 0xFF
boolean
True or false values
let isActive: boolean
isActive = true
isActive = false
null & undefined
Absence of value
let empty: null = null
let notSet: undefined
🔹 Array Types
Define arrays with specific element types:
// Array syntax
let numbers: number[] = [1, 2, 3, 4, 5]
let names: string[] = ['Alice', 'Bob', 'Charlie']
// Generic array syntax
let scores: Array<number> = [90, 85, 95]
let items: Array<string> = ['item1', 'item2']
// Multi-dimensional arrays
let matrix: number[][] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
// Mixed type arrays (use union types)
let mixed: (string | number)[] = ['hello', 42, 'world', 100]
Output:
numbers: [1, 2, 3, 4, 5] names: ['Alice', 'Bob', 'Charlie']
🔹 Object Types
Define structure for objects:
// Object type annotation
let user: { name: string; age: number } = {
name: 'John',
age: 30
}
// Optional properties
let person: {
name: string
age?: number // Optional
email?: string
} = {
name: 'Alice'
}
// Readonly properties
let config: {
readonly apiKey: string
timeout: number
} = {
apiKey: 'abc123',
timeout: 5000
}
// config.apiKey = 'new' // ✗ Error: readonly
// Index signatures
let dictionary: { [key: string]: string } = {
hello: 'world',
foo: 'bar'
}
🔹 Special Types
TypeScript's special built-in types:
// any - Disables type checking (avoid when possible)
let anything: any = 'hello'
anything = 42
anything = true
// unknown - Type-safe alternative to any
let value: unknown = 'hello'
// value.toUpperCase() // ✗ Error
if (typeof value === 'string') {
value.toUpperCase() // ✓ Works after type check
}
// void - No return value
function logMessage(msg: string): void {
console.log(msg)
// No return statement
}
// never - Never returns
function throwError(message: string): never {
throw new Error(message)
}
// object - Non-primitive type
let obj: object = { name: 'John' }
obj = [1, 2, 3] // Arrays are objects
obj = new Date() // Dates are objects
🔹 Tuple Types
Fixed-length arrays with specific types:
// Basic tuple
let person: [string, number] = ['Alice', 25]
// Accessing tuple elements
let name = person[0] // string
let age = person[1] // number
// Tuple with optional elements
let point: [number, number, number?] = [10, 20]
point = [10, 20, 30] // Also valid
// Readonly tuples
let rgb: readonly [number, number, number] = [255, 0, 0]
// rgb[0] = 100 // ✗ Error: readonly
// Named tuples (TypeScript 4.0+)
let user: [name: string, age: number, active: boolean] = [
'John',
30,
true
]
// Rest elements in tuples
let scores: [string, ...number[]] = ['Math', 90, 85, 95]
🔹 Enum Types
Define named constants:
// Numeric enum
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
let move: Direction = Direction.Up
// String enum
enum Status {
Active = 'ACTIVE',
Inactive = 'INACTIVE',
Pending = 'PENDING'
}
let userStatus: Status = Status.Active
// Heterogeneous enum (mixed)
enum Mixed {
No = 0,
Yes = 'YES'
}
// Const enum (optimized)
const enum Color {
Red,
Green,
Blue
}
let color: Color = Color.Red
🔹 Utility Types
Built-in helper types for transformations:
interface User {
id: number
name: string
email: string
age: number
}
// Partial - Make all properties optional
type PartialUser = Partial<User>
const update: PartialUser = { name: 'John' }
// Required - Make all properties required
type RequiredUser = Required<User>
// Readonly - Make all properties readonly
type ReadonlyUser = Readonly<User>
// Pick - Select specific properties
type UserPreview = Pick<User, 'id' | 'name'>
const preview: UserPreview = { id: 1, name: 'Alice' }
// Omit - Exclude specific properties
type UserWithoutEmail = Omit<User, 'email'>
// Record - Create object type with specific keys
type Roles = Record<string, boolean>
const permissions: Roles = {
read: true,
write: false
}