TypeScript Types

Page 3 of 4

Number

Represents numeric values, including integers and floats.

The number type in TypeScript is used for all numeric values. This includes integers, floating-point numbers, and special values like Infinity, -Infinity, and NaN.

Example of usage

let age: number = 30;
let price: number = 19.99;
let hexValue: number = 0xf00d;

Notes

  • All numbers in TypeScript are floating-point values.
  • TypeScript also supports binary, octal, hexadecimal, and decimal literals.

Object

Represents any value that is not a primitive.

The object type represents any non-primitive value. This means it can be any value except string, number, boolean, symbol, null, or undefined. It is useful for describing APIs that expect an object but don’t care about its specific properties.

Example of usage

let user: object;

user = { name: "John", age: 30 }; // OK

// user = "hello"; // Error: Type 'string' is not assignable to type 'object'.

Notes

  • The object type is not the same as {} (the empty object type) or Object.
  • It’s generally more useful to define a specific object shape with an interface or type alias rather than using the generic object type.

String

Represents textual data, such as "hello, world".

The string type in TypeScript is one of the primitive types and is used to work with textual data. You can assign any string value to a variable of type string.

Example of usage

let myName: string = "Alice";
let greeting: string = `Hello, ${myName}!`;

Notes

  • Strings can be enclosed in single quotes (') or double quotes (").
  • Template strings (using backticks `) are also strings, and they allow for embedded expressions and multi-line text.