Explore our collection of TypeScript tips, organized by category. Click on a tip to see more details.
TypeScript can infer types automatically in many cases.
Union types allow a value to be one of several types.
Create a new name for a type using type aliases.
Interfaces define the structure of objects.
TypeScript has two special types, null and undefined, for values that aren't there.
Make properties optional using the ? operator.
Use readonly to prevent properties from being changed.
Define types for functions.
Enums allow us to define a set of named constants.
Tuples let you express an array with a fixed number of elements whose types are known.
Type assertions are a way to tell the compiler 'trust me, I know what I'm doing.'
A literal is a more concrete sub-type of a collective type.
Index signatures are used for objects without a defined list of properties.
Create reusable components that can work with a variety of types.
The never type represents the type of values that never occur.
Intersection types combine multiple types into one.
The keyof operator takes an object type and produces a string or numeric literal union of its keys.
A mapped type is a generic type which uses a union of PropertyKeys to iterate through keys to create a type.
Conditional types select one of two possible types based on a condition expressed as a type relationship test.
Type guards are some expression that performs a runtime check that guarantees the type in some scope.
Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.
Mixins are a way to create classes from reusable components.
Namespaces are used to organize code into logical groups and to prevent name collisions in your code.
Symbols are primitive values guaranteed to be unique, even if they have the same description.
Template literal types build on string literal types, and have the ability to expand into many strings via unions.
Partial<T> constructs a type with all properties of T set to optional.
Required<T> constructs a type consisting of all properties of T set to required.
Pick<T, K> constructs a type by picking the set of properties K from T.
Omit<T, K> constructs a type by picking all properties from T and then removing K.
ReturnType<T> constructs a type consisting of the return type of function T.