Primitive Types
TypeScript provides several primitive types that you can use to declare variables. Here are the most common ones:
Number: Used for both integers and floating-point values. TypeScript doesn’t differentiate between integers and floats; all numbers are simply type number.
let age: number = 25;
let price: number = 99.99;
String: Represents textual data. It’s similar to strings in JavaScript.
let name: string = "Alice";
let greeting: string = "Hello, World!";
Boolean: Represents a logical entity and can have only two values: true or false
let isCompleted: boolean = false;
let isValid: boolean = true;
Null: Intentional absence of any object value.
let empty: null = null;
Undefined: Indicates that a variable has not been assigned a value.
let notDefined: undefined;
Union Types
Union types allow you to define a variable that can hold more than one type of value.
let id: number | string;
id = 101; // OK
id = "202"; // Also OK
// id = true; // Error: Type 'boolean' is not assignable to type 'number | string'.
Custom Types
You can create custom types using interfaces or type aliases.
Interface: Defines the shape of an object. In TypeScript, an interface is an abstract type that defines the contract for the shape of an object. It specifies which property names a given object can have.
interface User {
name: string;
age: number;
}
let user: User = {
name: "Bob",
age: 30
};
Here are some key points about interfaces in TypeScript:
Shape Definition: An interface describes the structure or skeleton of an object. It enforces a specific syntax on classes, specifying the types of data an object must have. Essentially, an interface acts as a contract that outlines the shape of an object.
Type Checking: Interfaces are used for type checking during development.
When you define an object with properties, TypeScript implicitly creates an interface based on the property names and data types.
The compiler ensures that objects conform to the defined interface.
Let’s say we have an object with a label
property of type string
:
function printLabel(labeledObj: { label: string }) {
console.log(labeledObj.label);
}
We can achieve the same using an interface:
interface LabeledValue {
label: string;
}
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
The interface LabeledValue
represents the requirement of having a label
property of type string
. The order of properties doesn’t matter; only the shape does.
Arrays and Tuples
Arrays can hold multiple values of the same type, while tuples can contain a fixed number of elements with different types.
Arrays:
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Anna", "John", "Sarah"];
Tuple:
let person: [string, number] = ["Steve", 25]; // Tuple for a string and a number
;
Enum
Enums allow you to define a set of named constants.
enum Color {
Red,
Green,
Blue
}
let c: Color = Color.Green;
Any, Unknown, and Never
Any: Can be anything, try to avoid using it.
let notSure: any = 4;
notSure = "maybe a string";
Unknown: Safer than any, requires type checking.
let result: unknown;
// You need to ensure the type before you can assign `result` to other typed variables or use it.
Never: Represents values that never occur.
function error(message: string): never {
throw new Error(message);
}
These are the basics of TypeScript data types. For a more comprehensive tutorial, you can visit the TypeScript Documentation or follow interactive tutorials like the one provided by W3Schools.