TypeScript Data Types
Types are at the core of TypeScript. They describe what kind of values variables, function parameters, and function return values can have. By specifying types, we can have the TypeScript compiler help us check code correctness.
TypeScript supports all basic JavaScript data types and adds some new types on top of them.
Primitive Types
1. Boolean
The most basic data type, with only two values: true and false.
let isDone: boolean = false;2. Number
Like JavaScript, all numbers in TypeScript are floating-point numbers. The type of these numbers is number. Supports decimal, hexadecimal, binary, and octal literals.
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;3. String
Used to represent text data. Represented by string. Can use double quotes (") or single quotes (') to enclose strings.
let color: string = "blue";
color = 'red';Also supports template strings (using backticks `), which can embed expressions and span multiple lines.
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${fullName}.
I'll be ${age + 1} years old next month.`;Composite Types
4. Array
TypeScript allows you to define arrays in two ways.
First, add [] after the element type to indicate an array composed of elements of this type:
let list: number[] = [1, 2, 3];Second, use the array generic, Array<element type>:
let list: Array<number> = [1, 2, 3];5. Tuple
Tuple types allow you to express an array with a known number of elements and types, where the types of each element don't have to be the same.
// Declare a tuple type
let x: [string, number];
// Initialize
x = ['hello', 10]; // OK
// x = [10, 'hello']; // ErrorSpecial Types
6. Any
Sometimes, we don't want the type checker to check a value, but want it to pass through the compilation stage directly. In this case, we can use the any type to mark these variables. The any type can be a value of any type.
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a booleanUsing any loses most of the advantages TypeScript brings, so it should be used cautiously.
7. Void
The void type is the opposite of the any type, representing no type at all. When a function has no return value, you'll typically see its return value type is void.
function warnUser(): void {
console.log("This is my warning message");
}8. Null and Undefined
In TypeScript, null and undefined each have their own types, called null and undefined respectively. By default, they are subtypes of all types, meaning you can assign null and undefined to number type variables.
let u: undefined = undefined;
let n: null = null;When you enable the "strictNullChecks": true configuration in tsconfig.json, null and undefined can only be assigned to void and their respective types.
9. Never
The never type represents the type of values that never exist. For example, never is the return value type of function expressions or arrow function expressions that always throw exceptions or have no return value at all.
// Functions returning never must have an unreachable endpoint
function error(message: string): never {
throw new Error(message);
}
function infiniteLoop(): never {
while (true) {}
}10. Object
object represents non-primitive types, that is, types other than number, string, boolean, symbol, null, or undefined. Using the object type, you can better represent APIs like Object.create.
declare function create(o: object | null): void;
create({ prop: 0 }); // OK
create(null); // OK
// create(42); // Error
// create("string"); // Error