-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from aelshinawy/main
[Snippets] Added some helper types for Typescript
- Loading branch information
Showing
10 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: At Least One Key | ||
description: Ensures that at least one property of an object is required. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition | ||
--- | ||
|
||
```ts | ||
type AtLeastOne<T> = { | ||
[K in keyof T]: Pick<T, K> & Partial<Omit<T, K>>; | ||
}[keyof T]; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id?: string; | ||
name?: string; | ||
isActive?: boolean; | ||
}; | ||
|
||
type AtLeastOneA = AtLeastOne<A>; | ||
// Requires at least one of 'id', 'name', or 'isActive' to be defined | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: Deep Partial Type | ||
description: Converts all properties of a type, including nested objects, into optional. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,optional | ||
--- | ||
|
||
```ts | ||
type DeepPartial<T> = { | ||
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]; | ||
}; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
name: string; | ||
details: { | ||
age: number; | ||
address: { city: string; zip: string }; | ||
}; | ||
}; | ||
|
||
type PartialA = DeepPartial<A>; | ||
/* | ||
Type PartialA: | ||
{ | ||
name?: string; | ||
details?: { | ||
age?: number; | ||
address?: { city?: string; zip?: string }; | ||
}; | ||
} | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: Deep Readonly Type | ||
description: Converts all properties of a type, including nested objects, into readonly. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,readonly | ||
--- | ||
|
||
```ts | ||
type DeepReadonly<T> = { | ||
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K]; | ||
}; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
name: string; | ||
details: { | ||
age: number; | ||
address: { city: string; zip: string }; | ||
}; | ||
}; | ||
|
||
type ReadonlyA = DeepReadonly<A>; | ||
/* | ||
Type ReadonlyA: | ||
{ | ||
readonly name: string; | ||
readonly details: { | ||
readonly age: number; | ||
readonly address: { readonly city: string; readonly zip: string }; | ||
}; | ||
} | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Deep Required Type | ||
description: Converts all properties of a type, including nested objects, into required. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,required | ||
--- | ||
|
||
```ts | ||
type DeepRequired<T> = T extends object | ||
? { [K in keyof T]-?: DeepRequired<T[K]> } | ||
: T; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id?: string; | ||
name?: string; | ||
details?: { | ||
age?: number; | ||
address?: { city?: string; zip?: string }; | ||
}; | ||
}; | ||
|
||
type RequiredA = DeepRequired<A>; | ||
// Result: { id: string; name: string; details: { age: number; address: { city: string; zip: string }; }; } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: Keys of Type | ||
description: Extracts keys from an object type that match a specified value type. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition | ||
--- | ||
|
||
```ts | ||
type KeysOfType<T, U> = { [K in keyof T]: T[K] extends U ? K : never }[keyof T]; | ||
|
||
|
||
// Usage: | ||
type A = { name: string; age: number; isActive: boolean, isDeleted: boolean }; | ||
type StringKeys = KeysOfType<A, string>; // "name" | ||
type BooleanKeys = KeysOfType<A, boolean>; // "isActive" | "isDeleted" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Keys to Optional | ||
description: Makes only the specified keys of an object type optional. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,optional | ||
--- | ||
|
||
```ts | ||
type OptionalKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id: string; | ||
name: string; | ||
age: number; | ||
}; | ||
|
||
type WithOptionalName = OptionalKeys<A, "name">; | ||
// { id: string; age: number; name?: string } | ||
|
||
type WithOptionalNameAndAge = OptionalKeys<A, "name" | "age">; | ||
// Result: { id: string; name?: string; age?: number } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Nullable Keys | ||
description: Extracts keys from an object type that allow null or undefined values. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,nullable | ||
--- | ||
|
||
```ts | ||
type NullableKeys<T> = { | ||
[K in keyof T]: null extends T[K] ? K : undefined extends T[K] ? K : never; | ||
}[keyof T]; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id: string; | ||
name?: string; | ||
description: string | null; | ||
}; | ||
|
||
type Nullable = NullableKeys<A>; // "name" | "description" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Omit Keys of Type | ||
description: Removes keys of a specified type from an object type. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,omit,keys | ||
--- | ||
|
||
```ts | ||
type OmitKeysOfType<T, U> = { | ||
[K in keyof T as T[K] extends U ? never : K]: T[K]; | ||
}; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id: string; | ||
isActive: boolean; | ||
data: number[]; | ||
}; | ||
|
||
type WithoutBoolean = OmitKeysOfType<A, boolean>; // { id: string; data: number[] } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Required Keys | ||
description: Extracts required keys from an object. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,required | ||
--- | ||
|
||
```ts | ||
type RequiredKeys<T> = { | ||
[K in keyof T]-?: {} extends Pick<T, K> ? never : K; | ||
}[keyof T]; | ||
|
||
|
||
// Usage: | ||
type A = { | ||
id: string; | ||
name?: string; | ||
isActive: boolean; | ||
}; | ||
|
||
type ReqKeys = RequiredKeys<A>; // "id" | "isActive" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: Union to Intersection | ||
description: Converts a union type into an intersection type. | ||
author: aelshinawy | ||
tags: typescript,helper-types,typedefinition,intersection,union | ||
--- | ||
|
||
```ts | ||
type UnionToIntersection<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void | ||
? I | ||
: never; | ||
|
||
|
||
// Usage: | ||
type A = { id: string }; | ||
type B = { name: string }; | ||
type C = { age: number }; | ||
|
||
type Intersected = UnionToIntersection<A | B | C>; | ||
// { id: string } & { name: string } & { age: number } | ||
``` |