https://github.com/bodnya29179/typescript-types-deep-dive
🕵️♂️ Dive into advanced TypeScript types – Utility Types, Generics, Operators, and more. Master type theory through concise examples and hands-on exercises.
https://github.com/bodnya29179/typescript-types-deep-dive
deep-dive generics types typescript
Last synced: 10 months ago
JSON representation
🕵️♂️ Dive into advanced TypeScript types – Utility Types, Generics, Operators, and more. Master type theory through concise examples and hands-on exercises.
- Host: GitHub
- URL: https://github.com/bodnya29179/typescript-types-deep-dive
- Owner: bodnya29179
- Created: 2023-08-19T11:51:46.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-29T22:28:36.000Z (over 1 year ago)
- Last Synced: 2025-04-09T23:50:35.504Z (over 1 year ago)
- Topics: deep-dive, generics, types, typescript
- Language: TypeScript
- Homepage:
- Size: 132 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.

## 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.
```typescript
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.
```typescript
keyof Something
```
### \> Typeof
`typeof` is a keyword which is used to check the type of variable.
```typescript
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.
```typescript
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.
```typescript
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.
```typescript
const userPermissions = ['admin', 'user'] as const;
```
### \> Tuples
`Tuple` is a typed array with a pre-defined length and types for each index.
```typescript
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.
```typescript
type LiteralType = `${Type1}-${Type2}`;
```
### \> Utility types
#### Partial
`Partial` changes all the properties in an object to be optional.
```typescript
Partial
```
#### Required
`Required` changes all the properties in an object to be required.
```typescript
Required
```
#### Record
`Record` is a shortcut to defining an object type with a specific key type and value type.
```typescript
Record
```
#### Omit
`Omit` removes keys from an object type.
```typescript
Omit
```
#### Pick
`Pick` removes all but the specified keys from an object type.
```typescript
Pick
```
#### Exclude
`Exclude` removes types from a union.
```typescript
Exclude
```
#### Extract
`Extract` constructs a type by extracting from `Type` all union members that are assignable to `UnionType`.
```typescript
Extract
```
#### ReturnType
`ReturnType` extracts the return type of function type.
```typescript
ReturnType
```
#### Parameters
`Parameters` extracts the parameter types of a function type as an array.
```typescript
Parameters
```
#### Readonly
`Readonly` is used to create a new type where all properties are readonly, meaning they cannot be modified once assigned
a value.
```typescript
Parameters
```
#### 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.
```typescript
Awaited
```
#### Other utilities for self-processing:
```typescript
Extract
```
```typescript
NonNullable
```
```typescript
InstanceType
```
```typescript
Uppercase
```
```typescript
Lowercase
```
```typescript
Capitalize
```
```typescript
Uncapitalize
```
### \> Indexed access types
`Indexed access types` can be used to look up a specific property on another type.
```typescript
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](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html).
2. [Utility Types](https://www.typescriptlang.org/docs/handbook/utility-types.html).
3. [TypeScript Book](https://github.com/gibbok/typescript-book).
4. [Indexed Access Types](https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html).
5. [Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html).
## Repository link
🔗 https://github.com/bodnya29179/TypeScript-Types-Deep-Dive
**or**
