Skip to content

Commit

Permalink
Merge pull request #239 from aelshinawy/main
Browse files Browse the repository at this point in the history
[Snippets] Added some helper types for Typescript
  • Loading branch information
Mathys-Gasnier authored Jan 26, 2025
2 parents cbb3118 + d2c7e92 commit df7dcbf
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 0 deletions.
23 changes: 23 additions & 0 deletions snippets/typescript/helper-types/at-least-one-key.md
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
```
34 changes: 34 additions & 0 deletions snippets/typescript/helper-types/deep-partial-type.md
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 };
};
}
*/
```
34 changes: 34 additions & 0 deletions snippets/typescript/helper-types/deep-readonly-type.md
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 };
};
}
*/
```
26 changes: 26 additions & 0 deletions snippets/typescript/helper-types/deep-required-type.md
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 }; }; }
```
16 changes: 16 additions & 0 deletions snippets/typescript/helper-types/keys-of-type.md
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"
```
24 changes: 24 additions & 0 deletions snippets/typescript/helper-types/keys-to-optional.md
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 }
```
22 changes: 22 additions & 0 deletions snippets/typescript/helper-types/nullable-keys.md
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"
```
22 changes: 22 additions & 0 deletions snippets/typescript/helper-types/omit-keys-of-type.md
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[] }
```
22 changes: 22 additions & 0 deletions snippets/typescript/helper-types/required-keys.md
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"
```
21 changes: 21 additions & 0 deletions snippets/typescript/helper-types/union-to-intersection.md
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 }
```

0 comments on commit df7dcbf

Please sign in to comment.