CPCODELAB
TypeScript

Primitive Types

9 min read

Primitive Types

TypeScript provides type annotations for the three JavaScript primitives you use every day: string, number, and boolean. You write the type after the variable name, separated by a colon.

ts
let username: string = "Alice";
let age: number = 30;
let isActive: boolean = true;

// TypeScript infers these without annotations too:
let city = "Bangalore"; // inferred as string
let score = 100;         // inferred as number

Other primitives include bigint for arbitrarily large integers and symbol for unique identifiers. The special values null and undefined each have their own types. With strictNullChecks enabled, a variable typed string cannot hold null — you must explicitly opt in with a union type.

ts
let id: bigint = 9007199254740993n;
let key: symbol = Symbol("apiKey");

let maybeNull: string | null = null; // OK
let definite: string = null;         // Error with strictNullChecks

Prefer letting TypeScript infer types for local variables with obvious initializers. Reserve explicit annotations for function signatures, return types, and public API boundaries where the intent needs to be clear.

Ready to test yourself?
Practice TypeScript— quiz & coding exercises