![]() |
VOOZH | about |
TypeScript is a strongly typed, object-oriented, compiled programming language developed and maintained by Microsoft. It is a superset of JavaScript, adding static types and other powerful features to improve development efficiency and code quality. TypeScript is widely used in web development, especially in large-scale applications.
Here are some of the main features of TypeScript:
Hereβs a complete TypeScript cheat sheet that covers everything from installation to advanced topics, and examples.
To use TypeScript, you need to install it globally or locally in your project.
npm install -g typescriptnpm install typescript --save-devtsc --versiontsc filename.tsFor more details, you can explore this article: How to Install TypeScript.
TypeScript provides basic types to define variables with specific data types.
| Type | Description | Example |
|---|---|---|
boolean | Represents true/false values. | let isDone: boolean = false; |
number | Represents both integers and floating-point numbers. | let count: number = 42; |
string | Represents textual data. | let name: string = "TypeScript"; |
number[] | Represents an array of numbers. | let list: number[] = [1, 2, 3]; |
[string, number] | Represents a tuple (fixed-type array). | let tuple: [string, number] = ["hello", 10]; |
any | Represents a dynamic type (use sparingly). | let notSure: any = 4; |
void | Represents the absence of a type (used for functions that return nothing). | let nothing: void = undefined; |
undefined | Represents an uninitialized variable. | let u: undefined = undefined; |
null | Represents an intentional absence of an object value. | let n: null = null; |
TypeScript supports advanced types for more complex scenarios.
Union Types allows a variable to hold multiple types.
Combines multiple types into one.
Restricts a variable to a set of specific values.
Type aliases in TypeScript allow you to create a new name for an existing type
Functions in TypeScript can have typed parameters and return values.
Typed functions in TypeScript allow you to define the types of parameters a function accepts and the type of value it returns.
Optional parameters (using ?) allow omitting the argument in function calls, resulting in undefined within the function. Default parameters (using = value) provide a fallback if the argument is not provided.
Arrow functions provide a more concise syntax for defining functions compared to traditional function expressions.
Interfaces in TypeScript define a contract or shape for data. They specify the properties (and sometimes methods) that an object should have.
Classes in TypeScript provide a blueprint for creating objects. They encapsulate data (properties) and behavior (methods) into a single unit.
Generics in TypeScript allow you to write reusable components that can work with a variety of types without sacrificing type safety.
Utility types in TypeScript provide a set of pre-defined type transformations that perform common operations on types.
Partial<T> is a utility type that takes a type T and constructs a new type where all properties of T are optional.
Readonly<T> is a utility type that takes a type T and creates a new type where all properties of T are read-only.
Record<K, T> is a utility type that constructs an object type whose property keys are K and whose property values are T. K can be a string, number, or symbol literal, and T can be any type.
Pick<T, K> constructs a new type by picking a set of properties K (which is a union of string literals) from type T
Type assertions in TypeScript allow you to override the compiler's type inference and explicitly tell it what type a value is.
Modules in TypeScript allow you to organize your code into separate files, improving code maintainability and reusability.