TypeScript: Type Syntax

By Xah Lee. Date: . Last updated: .

Type Declaration Syntax

the form is:

:TypeSpec

placed after a variable name.

Example

// vector addition
const f_vec_add =
  ((
    [a1, a2]: [number, number],
    [b1, b2]: [number, number],
  ): [number, number] => [a1 + b1, a2 + b2]);
const f_vec_length = ((v: number[]) => Math.hypot(...v));

Type Syntax

here are the syntax for the type spec.

Type Spec Syntax for JavaScript Primitive Types

Array Type

Object Type

Optional Property

add a question mark to the end of variable name.

Union Types

Union Types means one or the other.

can be more than two items.

TypeScript, type sytax