Primitive Types

`string`, `number`, `boolean`, and Friends

Primitive Types

TypeScript has a type for each JavaScript primitive. Lowercase names, simple annotations.

3 min read Level 1/5 #typescript#primitives#string
What you'll learn
  • Annotate variables with primitive types
  • Tell `string` from `String`
  • Recognize the less common ones

JavaScript primitives have lowercase type names in TypeScript: string, number, boolean, bigint, symbol, null, undefined.

Basic Annotations

let name: string = "Ada";
let age: number = 36;
let admin: boolean = true;

let count: bigint = 10n;
let id: symbol = Symbol("id");

let nothing: null = null;
let notYet: undefined = undefined;

The pattern is let name: Type = value. The : Type is the annotation.

string vs String

Use lowercase. The capitalized String, Number, Boolean refer to the wrapper-object types — almost never what you want.

let a: string = "hi";        // ✓ Primitive
let b: String = "hi";        // ✗ Don't — refers to the wrapper object

You Usually Don’t Annotate

TypeScript infers the type from the value. If you wrote:

let name = "Ada";

name’s type is already string. You only need to annotate when:

  • The variable starts uninitialized
  • You want a wider or different type than the initial value
  • The function signature requires it (for parameters and returns)

We cover inference in a couple of lessons.

null and undefined

By default (with strictNullChecks on, which is part of strict), null and undefined are NOT assignable to other types:

let name: string = null;
//  ~~~~                  Type 'null' is not assignable to type 'string'.

To allow them, use a union:

let maybeName: string | null = null;

(Unions come up next chapter; just know the syntax.)

bigint and symbol

Real types but rare in everyday code:

  • bigint — large integers, with the n suffix: 10n
  • symbol — unique identifiers (Symbol("id"))

You’ll mostly see these in library types, not in app code.

Up Next

Arrays — T[] or Array<T>.

Arrays →