It equips you with key insights and skills to effectively utilize TypeScript types for enhanced code quality and efficiency.
-
Important TypeScript topics for mastering further higher-level topics.
Files:
00-*.ts
-12-*.ts
. -
Top-level topics about types in TypeScript.
Files:
13-*.ts
-18-*.ts
. -
Tasks that you can practice and think about.
Types union
is a way to declare a type that can hold values of multiple specified types, allowing for greater
flexibility in variable and parameter declarations.
interface Car {
type: 'car';
brand: 'mercedes';
}
interface Bike {
type: 'bike';
model: 'mountain';
}
type VehicleType = Car | Bike;
const vehicle: VehicleType = {
type: 'car',
brand: 'mercedes',
};
keyof
is a keyword which is used to extract the key type from an object type.
keyof Something
typeof
is a keyword which is used to check the type of variable.
typeof Something
Types intersection
is a way to declare a type that combines multiple types into a single type that contains all the
properties and characteristics of each individual type.
interface Person {
name: string;
}
interface Address {
street: string;
city: string;
}
type PersonWithAddressType = Person & Address;
const person: PersonWithAddressType = {
name: 'John',
street: 'Main St.',
city: 'New York',
};
Types assertion
is the explicit specification of a value's type to temporarily override the type inferred by the
TypeScript compiler, aiding in type-safe interactions with values.
interface User {
name: string;
}
const user = {
name: 'John',
age: 15,
} as User;
Const assertion
is a way to tell the type system that a variable should not have its type widened,
preserving its literal value as the most specific type.
const userPermissions = ['admin', 'user'] as const;
Tuple
is a typed array with a pre-defined length and types for each index.
const person: [string, number, boolean] = ['John', 20, true];
Literal Types
allows us to create more precise types like type combinations using template literals.
type LiteralType = `${Type1}-${Type2}`;
Partial
changes all the properties in an object to be optional.
Partial<Type>
Required
changes all the properties in an object to be required.
Required<Type>
Record
is a shortcut to defining an object type with a specific key type and value type.
Record<KeyType, ValueType>
Omit
removes keys from an object type.
Omit<Type, Keys>
Pick
removes all but the specified keys from an object type.
Pick<Type, Keys>
Exclude
removes types from a union.
Exclude<UnionType, ExcludedMembers>
Extract
constructs a type by extracting from Type
all union members that are assignable to UnionType
.
Extract<Type, UnionType>
ReturnType
extracts the return type of function type.
ReturnType<Type>
Parameters
extracts the parameter types of a function type as an array.
Parameters<Type>
Readonly
is used to create a new type where all properties are readonly, meaning they cannot be modified once assigned
a value.
Parameters<Type>
Readonly
is meant to model operations like await in async functions, or the .then() method on Promises - specifically,
the way that they recursively unwrap Promises.
Awaited<Type>
Extract<Type, Union>
NonNullable<Type>
InstanceType<Type>
Uppercase<StringType>
Lowercase<StringType>
Capitalize<StringType>
Uncapitalize<StringType>
Indexed access types
can be used to look up a specific property on another type.
Type['property']
Generics
are a mechanism that allows you to create generic types or functions that can work with different data types
while maintaining type safety. They allow you to provide compile-time type checking and use this type information to
improve data handling.
π https://github.com/bodnya29179/TypeScript-Types-Deep-Dive
or