Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2c7e92

Browse files
added multiple helper typescript types
1 parent 27d8016 commit d2c7e92

File tree

10 files changed

+244
-0
lines changed

10 files changed

+244
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: At Least One Key
3+
description: Ensures that at least one property of an object is required.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition
6+
---
7+
8+
```ts
9+
type AtLeastOne<T> = {
10+
[K in keyof T]: Pick<T, K> & Partial<Omit<T, K>>;
11+
}[keyof T];
12+
13+
14+
// Usage:
15+
type A = {
16+
id?: string;
17+
name?: string;
18+
isActive?: boolean;
19+
};
20+
21+
type AtLeastOneA = AtLeastOne<A>;
22+
// Requires at least one of 'id', 'name', or 'isActive' to be defined
23+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Deep Partial Type
3+
description: Converts all properties of a type, including nested objects, into optional.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,optional
6+
---
7+
8+
```ts
9+
type DeepPartial<T> = {
10+
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
11+
};
12+
13+
14+
// Usage:
15+
type A = {
16+
name: string;
17+
details: {
18+
age: number;
19+
address: { city: string; zip: string };
20+
};
21+
};
22+
23+
type PartialA = DeepPartial<A>;
24+
/*
25+
Type PartialA:
26+
{
27+
name?: string;
28+
details?: {
29+
age?: number;
30+
address?: { city?: string; zip?: string };
31+
};
32+
}
33+
*/
34+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Deep Readonly Type
3+
description: Converts all properties of a type, including nested objects, into readonly.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,readonly
6+
---
7+
8+
```ts
9+
type DeepReadonly<T> = {
10+
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
11+
};
12+
13+
14+
// Usage:
15+
type A = {
16+
name: string;
17+
details: {
18+
age: number;
19+
address: { city: string; zip: string };
20+
};
21+
};
22+
23+
type ReadonlyA = DeepReadonly<A>;
24+
/*
25+
Type ReadonlyA:
26+
{
27+
readonly name: string;
28+
readonly details: {
29+
readonly age: number;
30+
readonly address: { readonly city: string; readonly zip: string };
31+
};
32+
}
33+
*/
34+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Deep Required Type
3+
description: Converts all properties of a type, including nested objects, into required.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,required
6+
---
7+
8+
```ts
9+
type DeepRequired<T> = T extends object
10+
? { [K in keyof T]-?: DeepRequired<T[K]> }
11+
: T;
12+
13+
14+
// Usage:
15+
type A = {
16+
id?: string;
17+
name?: string;
18+
details?: {
19+
age?: number;
20+
address?: { city?: string; zip?: string };
21+
};
22+
};
23+
24+
type RequiredA = DeepRequired<A>;
25+
// Result: { id: string; name: string; details: { age: number; address: { city: string; zip: string }; }; }
26+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Keys of Type
3+
description: Extracts keys from an object type that match a specified value type.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition
6+
---
7+
8+
```ts
9+
type KeysOfType<T, U> = { [K in keyof T]: T[K] extends U ? K : never }[keyof T];
10+
11+
12+
// Usage:
13+
type A = { name: string; age: number; isActive: boolean, isDeleted: boolean };
14+
type StringKeys = KeysOfType<A, string>; // "name"
15+
type BooleanKeys = KeysOfType<A, boolean>; // "isActive" | "isDeleted"
16+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Keys to Optional
3+
description: Makes only the specified keys of an object type optional.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,optional
6+
---
7+
8+
```ts
9+
type OptionalKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
10+
11+
12+
// Usage:
13+
type A = {
14+
id: string;
15+
name: string;
16+
age: number;
17+
};
18+
19+
type WithOptionalName = OptionalKeys<A, "name">;
20+
// { id: string; age: number; name?: string }
21+
22+
type WithOptionalNameAndAge = OptionalKeys<A, "name" | "age">;
23+
// Result: { id: string; name?: string; age?: number }
24+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Nullable Keys
3+
description: Extracts keys from an object type that allow null or undefined values.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,nullable
6+
---
7+
8+
```ts
9+
type NullableKeys<T> = {
10+
[K in keyof T]: null extends T[K] ? K : undefined extends T[K] ? K : never;
11+
}[keyof T];
12+
13+
14+
// Usage:
15+
type A = {
16+
id: string;
17+
name?: string;
18+
description: string | null;
19+
};
20+
21+
type Nullable = NullableKeys<A>; // "name" | "description"
22+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Omit Keys of Type
3+
description: Removes keys of a specified type from an object type.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,omit,keys
6+
---
7+
8+
```ts
9+
type OmitKeysOfType<T, U> = {
10+
[K in keyof T as T[K] extends U ? never : K]: T[K];
11+
};
12+
13+
14+
// Usage:
15+
type A = {
16+
id: string;
17+
isActive: boolean;
18+
data: number[];
19+
};
20+
21+
type WithoutBoolean = OmitKeysOfType<A, boolean>; // { id: string; data: number[] }
22+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Required Keys
3+
description: Extracts required keys from an object.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,required
6+
---
7+
8+
```ts
9+
type RequiredKeys<T> = {
10+
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
11+
}[keyof T];
12+
13+
14+
// Usage:
15+
type A = {
16+
id: string;
17+
name?: string;
18+
isActive: boolean;
19+
};
20+
21+
type ReqKeys = RequiredKeys<A>; // "id" | "isActive"
22+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Union to Intersection
3+
description: Converts a union type into an intersection type.
4+
author: aelshinawy
5+
tags: typescript,helper-types,typedefinition,intersection,union
6+
---
7+
8+
```ts
9+
type UnionToIntersection<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void
10+
? I
11+
: never;
12+
13+
14+
// Usage:
15+
type A = { id: string };
16+
type B = { name: string };
17+
type C = { age: number };
18+
19+
type Intersected = UnionToIntersection<A | B | C>;
20+
// { id: string } & { name: string } & { age: number }
21+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /