TypeScript supports all basic types found in JavaScript:
let age: number = 30;
let isStudent: boolean = false;
let name: string = "Alice";
let empty: null = null;
let notDefined: undefined = undefined;
let bigValue: bigint = 123n;
let uniqueId: symbol = Symbol("id");Arrays can be declared in two ways:
let scores: number[] = [90, 80, 70];
let names: Array<string> = ["Alice", "Bob"];Tuples are arrays with fixed length and known types:
let person: [string, number] = ["Alice", 30];Each element is typed specifically, making tuples ideal for structured data.
Enums are used for named constants:
enum Direction {
Up,
Down,
Left,
Right
}
let dir: Direction = Direction.Up;Enums improve code clarity and prevent magic numbers or strings.
let anything: any = 5;
anything = "hello";
let unsure: unknown = "value"; // safer than any
function log(): void {
console.log("Logging");
}
function error(): never {
throw new Error("Something went wrong");
}any: disables type checking.unknown: requires checking before use.void: used in functions that return nothing.never: for functions that never return.Used to tell the compiler the exact type of a variable:
let value: any = "Hello";
let strLength: number = (value as string).length;
// or using angle bracket
let length2: number = (<string>value).length;any for better safety.Ask the AI if you need help understanding or want to dive deeper in any topic