Menu

TypeScript Types: string, number, boolean and More

The built-in TypeScript types: string, number, boolean, bigint, symbol, null and undefined, plus arrays and objects at a glance. How to write a type annotation, why there is no integer type, and why you write string instead of String.

This page includes runnable editors - edit, run, and see output instantly.

TypeScript types describe what kind of value a variable can hold. You write a type after a colon, let name: string, and the compiler rejects any code that puts the wrong kind of value there. The basic types are the seven JavaScript primitives plus object types for arrays and objects.

The annotations are checked at compile time and then removed. The program that runs is plain JavaScript.

The Primitive Types

TypeExample valuesNotes
string"hi", 'hi', `hi ${name}`Text. All three quote styles have the same type.
number42, 3.14, -0.5, NaN, InfinityEvery number, whole or not. There is no int or float.
booleantrue, falseOnly these two values.
bigint10n, BigInt(10)Integers of any size. Needs target ES2020 or later.
symbolSymbol("id")A unique value, mostly used as an object key.
nullnull"Deliberately empty".
undefinedundefined"Not set". Also what a missing property reads as.

The type names are lowercase. typeof at runtime returns the same words for most of them, so typeof x === "number" is how you check a value's primitive type while the program runs.

Type Annotations

An annotation is : Type after a variable, a parameter or a function's parameter list. Parameters are where annotations matter most, because TypeScript cannot guess what a caller will pass.

When a variable is initialized on the same line, the annotation is usually redundant: let count = 10 already has type number. The rules for leaving types out are on the type inference page.

A Wrong Type Is a Compile Error

Assign a value of the wrong type and the compiler stops before anything runs:

The compiler prints:

index.ts(3,1): error TS2322: Type 'string' is not assignable to type 'number'.

The fix is to convert the value, port = Number("3000"), or to change the annotation if the variable really should hold text.

number: There Is No Integer Type

JavaScript stores every number as a 64-bit floating point value, so TypeScript has a single number type. Integers are exact up to Number.MAX_SAFE_INTEGER (2 ** 53 - 1). Past that, use bigint.

bigint and number do not mix: exact + 1 is a compile error (TS2365: Operator '+' cannot be applied to types 'bigint' and '1'., where '1' is the literal type of the 1) and, if it ever ran, a TypeError at runtime too. Convert one side explicitly with BigInt(1) or Number(exact), knowing that Number rounds values past the safe range.

If you want a type that only accepts whole numbers, TypeScript cannot express it for arbitrary values. Validate with Number.isInteger where the value enters your program.

boolean

boolean has exactly two values, true and false. Values that are merely truthy (1, "yes") are not booleans, so turn them into one with Boolean(x) or x !== 0.

null and undefined

With strict on (the default since TypeScript 6.0), null and undefined are separate types and are not part of string, number or any other type. A variable that may be empty says so with a union:

Reading a property of a value that might be undefined is a compile error until you check it. The null and undefined page covers the checks, ?. and ??.

Arrays and Objects at a Glance

Anything that is not a primitive is an object type. The three shapes you meet first:

let tags: string[] = ["ts", "js"];               // array of strings
let point: { x: number; y: number } = { x: 1, y: 2 }; // object with two number properties
let greet: (name: string) => string = (n) => `hi ${n}`; // function type

Each has its own page: arrays, tuples (fixed-length arrays), object types and function types.

string, number, boolean vs String, Number, Boolean

The capitalized names are the types of JavaScript's wrapper objects (new String("x")), not of ordinary values. Annotate with the lowercase names.

index.ts(3,7): error TS2322: Type 'String' is not assignable to type 'string'.
  'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

Change String to string on the first line and the block runs. The same applies to Number, Boolean, Symbol and BigInt. (Object is a different case: it accepts almost any value, primitives included; see object types.)

Other Built-in Types

A few more types come up early. Each has its own page.

TypeMeaning
anyTurns checking off for that value. Avoid it.
unknownAny value, but you must check it before using it.
voidA function returns nothing useful.
neverNo value at all: a function that always throws, or an impossible case.
objectAny non-primitive value.
"red" | "green"A literal type: only these exact values.

Frequently Asked Questions

Does TypeScript have an integer type?

No. number covers integers and decimals alike, because JavaScript stores every number as a 64-bit float. Check for a whole number at runtime with Number.isInteger(n), and use bigint (10n) when you need integers larger than Number.MAX_SAFE_INTEGER (2 ** 53 - 1) without losing precision.

What is the difference between string and String in TypeScript?

string is the primitive type, which is what string literals and template strings have. String is the type of the wrapper object created by new String("x"). Always annotate with the lowercase string, number and boolean; assigning a String to a string is a compile error (TS2322).

What are the primitive types in TypeScript?

The same seven as JavaScript: string, number, boolean, bigint, symbol, null and undefined. Everything else (arrays, objects, functions, class instances) is an object type.

How do you declare a variable with a type in TypeScript?

Put a colon and the type after the name: let count: number = 0;. For a function, annotate each parameter and optionally the return type: function add(a: number, b: number): number. When a variable is initialized, you can usually leave the annotation out and let TypeScript infer it.

Do TypeScript types exist at runtime?

No. The compiler checks the types and then removes them, so the JavaScript that runs has no annotations. At runtime you check values with JavaScript operators such as typeof, Array.isArray and instanceof.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED