Skip to content

πŸ•΅οΈβ€β™‚οΈ Dive into advanced TypeScript types – Utility Types, Generics, Operators, and more. Master type theory through concise examples and hands-on exercises.

Notifications You must be signed in to change notification settings

bodnya29179/TypeScript-Types-Deep-Dive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TypeScript: Types Deep Dive

Why do we need it?

It equips you with key insights and skills to effectively utilize TypeScript types for enhanced code quality and efficiency.

TypeScript "any"

Structure of files

  • Basics:

    Important TypeScript topics for mastering further higher-level topics.

    Files: 00-*.ts - 12-*.ts.

  • Advanced:

    Top-level topics about types in TypeScript.

    Files: 13-*.ts - 18-*.ts.

  • Homework:

    Tasks that you can practice and think about.

Definitions

> Types union

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

keyof is a keyword which is used to extract the key type from an object type.

keyof Something

> Typeof

typeof is a keyword which is used to check the type of variable.

typeof Something

> Types intersection

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

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

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;

> Tuples

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

Literal Types allows us to create more precise types like type combinations using template literals.

type LiteralType = `${Type1}-${Type2}`;

> Utility types

Partial

Partial changes all the properties in an object to be optional.

Partial<Type>

Required

Required changes all the properties in an object to be required.

Required<Type>

Record

Record is a shortcut to defining an object type with a specific key type and value type.

Record<KeyType, ValueType>

Omit

Omit removes keys from an object type.

Omit<Type, Keys>

Pick

Pick removes all but the specified keys from an object type.

Pick<Type, Keys>

Exclude

Exclude removes types from a union.

Exclude<UnionType, ExcludedMembers>

Extract

Extract constructs a type by extracting from Type all union members that are assignable to UnionType.

Extract<Type, UnionType>

ReturnType

ReturnType extracts the return type of function type.

ReturnType<Type>

Parameters

Parameters extracts the parameter types of a function type as an array.

Parameters<Type>

Readonly

Readonly is used to create a new type where all properties are readonly, meaning they cannot be modified once assigned a value.

Parameters<Type>

Awaited

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>

Other utilities for self-processing:

Extract<Type, Union>
NonNullable<Type>
InstanceType<Type>
Uppercase<StringType>
Lowercase<StringType>
Capitalize<StringType>
Uncapitalize<StringType>

> Indexed access types

Indexed access types can be used to look up a specific property on another type.

Type['property']

> Generics

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.

References

  1. Unions and Intersection Types.
  2. Utility Types.
  3. TypeScript Book.
  4. Indexed Access Types.
  5. Generics.

Repository link

πŸ”— https://github.com/bodnya29179/TypeScript-Types-Deep-Dive

or

Repo link

About

πŸ•΅οΈβ€β™‚οΈ Dive into advanced TypeScript types – Utility Types, Generics, Operators, and more. Master type theory through concise examples and hands-on exercises.

Topics

Resources

Stars

Watchers

Forks