Basic Types

1/2/2025

Basic Types

TypeScript supports the same basic types as JavaScript, plus some additional types for better type safety.

Primitive Types

typescript
// Boolean
let isDone: boolean = false

// Number
let decimal: number = 6
let hex: number = 0xf00d
let binary: number = 0b1010

// String
let color: string = 'blue'
let fullName: string = `Bob Smith`

Arrays

typescript
// Using type[]
let list: number[] = [1, 2, 3]

// Using Array<type>
let list2: Array<number> = [1, 2, 3]

Tuples

typescript
let x: [string, number]
x = ['hello', 10] // OK

Enums

typescript
enum Color {
  Red,
  Green,
  Blue
}
let c: Color = Color.Green

Any and Unknown

typescript
let notSure: any = 4
let uncertain: unknown = 'maybe'

// Type guards with unknown
if (typeof uncertain === 'string') {
  console.log(uncertain.toUpperCase())
}

Void and Never

typescript
function logMessage(message: string): void {
  console.log(message)
}

function throwError(message: string): never {
  throw new Error(message)
}