Movatterモバイル変換


[0]ホーム

URL:


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

Commitdf7dcbf

Browse files
Merge pull request#239 from aelshinawy/main
[Snippets] Added some helper types for Typescript
2 parentscbb3118 +d2c7e92 commitdf7dcbf

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+
typeAtLeastOne<T>= {
10+
[KinkeyofT]:Pick<T,K>&Partial<Omit<T,K>>;
11+
}[keyofT];
12+
13+
14+
// Usage:
15+
typeA= {
16+
id?:string;
17+
name?:string;
18+
isActive?:boolean;
19+
};
20+
21+
typeAtLeastOneA=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+
typeDeepPartial<T>= {
10+
[KinkeyofT]?:T[K]extendsobject?DeepPartial<T[K]>:T[K];
11+
};
12+
13+
14+
// Usage:
15+
typeA= {
16+
name:string;
17+
details: {
18+
age:number;
19+
address: { city:string; zip:string };
20+
};
21+
};
22+
23+
typePartialA=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+
typeDeepReadonly<T>= {
10+
readonly [KinkeyofT]:T[K]extendsobject?DeepReadonly<T[K]>:T[K];
11+
};
12+
13+
14+
// Usage:
15+
typeA= {
16+
name:string;
17+
details: {
18+
age:number;
19+
address: { city:string; zip:string };
20+
};
21+
};
22+
23+
typeReadonlyA=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+
typeDeepRequired<T>=Textendsobject
10+
? { [KinkeyofT]-?:DeepRequired<T[K]> }
11+
:T;
12+
13+
14+
// Usage:
15+
typeA= {
16+
id?:string;
17+
name?:string;
18+
details?: {
19+
age?:number;
20+
address?: { city?:string; zip?:string };
21+
};
22+
};
23+
24+
typeRequiredA=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+
typeKeysOfType<T,U>= { [KinkeyofT]:T[K]extendsU?K:never }[keyofT];
10+
11+
12+
// Usage:
13+
typeA= { name:string; age:number; isActive:boolean, isDeleted:boolean };
14+
typeStringKeys=KeysOfType<A,string>;// "name"
15+
typeBooleanKeys=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+
typeOptionalKeys<T,KextendskeyofT>=Omit<T,K>&Partial<Pick<T,K>>;
10+
11+
12+
// Usage:
13+
typeA= {
14+
id:string;
15+
name:string;
16+
age:number;
17+
};
18+
19+
typeWithOptionalName=OptionalKeys<A,"name">;
20+
// { id: string; age: number; name?: string }
21+
22+
typeWithOptionalNameAndAge=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+
typeNullableKeys<T>= {
10+
[KinkeyofT]:nullextendsT[K]?K:undefinedextendsT[K]?K:never;
11+
}[keyofT];
12+
13+
14+
// Usage:
15+
typeA= {
16+
id:string;
17+
name?:string;
18+
description:string|null;
19+
};
20+
21+
typeNullable=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+
typeOmitKeysOfType<T,U>= {
10+
[KinkeyofTasT[K]extendsU?never:K]:T[K];
11+
};
12+
13+
14+
// Usage:
15+
typeA= {
16+
id:string;
17+
isActive:boolean;
18+
data:number[];
19+
};
20+
21+
typeWithoutBoolean=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+
typeRequiredKeys<T>= {
10+
[KinkeyofT]-?: {}extendsPick<T,K>?never:K;
11+
}[keyofT];
12+
13+
14+
// Usage:
15+
typeA= {
16+
id:string;
17+
name?:string;
18+
isActive:boolean;
19+
};
20+
21+
typeReqKeys=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+
typeUnionToIntersection<U>= (Uextendsany? (arg:U)=>void:never)extends (arg:inferI)=>void
10+
?I
11+
:never;
12+
13+
14+
// Usage:
15+
typeA= { id:string };
16+
typeB= { name:string };
17+
typeC= { age:number };
18+
19+
typeIntersected=UnionToIntersection<A|B|C>;
20+
// { id: string } & { name: string } & { age: number }
21+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp