From d2c7e926c5c80c0c8421ff3a1307505aaadb1752 Mon Sep 17 00:00:00 2001 From: Ahmed El-Shinawy Date: Tue, 21 Jan 2025 02:20:04 +0200 Subject: [PATCH] added multiple helper typescript types --- .../helper-types/at-least-one-key.md | 23 +++++++++++++ .../helper-types/deep-partial-type.md | 34 +++++++++++++++++++ .../helper-types/deep-readonly-type.md | 34 +++++++++++++++++++ .../helper-types/deep-required-type.md | 26 ++++++++++++++ .../typescript/helper-types/keys-of-type.md | 16 +++++++++ .../helper-types/keys-to-optional.md | 24 +++++++++++++ .../typescript/helper-types/nullable-keys.md | 22 ++++++++++++ .../helper-types/omit-keys-of-type.md | 22 ++++++++++++ .../typescript/helper-types/required-keys.md | 22 ++++++++++++ .../helper-types/union-to-intersection.md | 21 ++++++++++++ 10 files changed, 244 insertions(+) create mode 100644 snippets/typescript/helper-types/at-least-one-key.md create mode 100644 snippets/typescript/helper-types/deep-partial-type.md create mode 100644 snippets/typescript/helper-types/deep-readonly-type.md create mode 100644 snippets/typescript/helper-types/deep-required-type.md create mode 100644 snippets/typescript/helper-types/keys-of-type.md create mode 100644 snippets/typescript/helper-types/keys-to-optional.md create mode 100644 snippets/typescript/helper-types/nullable-keys.md create mode 100644 snippets/typescript/helper-types/omit-keys-of-type.md create mode 100644 snippets/typescript/helper-types/required-keys.md create mode 100644 snippets/typescript/helper-types/union-to-intersection.md diff --git a/snippets/typescript/helper-types/at-least-one-key.md b/snippets/typescript/helper-types/at-least-one-key.md new file mode 100644 index 00000000..aadee83b --- /dev/null +++ b/snippets/typescript/helper-types/at-least-one-key.md @@ -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 = { + [K in keyof T]: Pick & Partial>; +}[keyof T]; + + +// Usage: +type A = { + id?: string; + name?: string; + isActive?: boolean; +}; + +type AtLeastOneA = AtLeastOne; +// Requires at least one of 'id', 'name', or 'isActive' to be defined +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/deep-partial-type.md b/snippets/typescript/helper-types/deep-partial-type.md new file mode 100644 index 00000000..e2f4b302 --- /dev/null +++ b/snippets/typescript/helper-types/deep-partial-type.md @@ -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 = { + [K in keyof T]?: T[K] extends object ? DeepPartial : T[K]; +}; + + +// Usage: +type A = { + name: string; + details: { + age: number; + address: { city: string; zip: string }; + }; +}; + +type PartialA = DeepPartial; +/* +Type PartialA: +{ + name?: string; + details?: { + age?: number; + address?: { city?: string; zip?: string }; + }; +} +*/ +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/deep-readonly-type.md b/snippets/typescript/helper-types/deep-readonly-type.md new file mode 100644 index 00000000..f1b3e971 --- /dev/null +++ b/snippets/typescript/helper-types/deep-readonly-type.md @@ -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 = { + readonly [K in keyof T]: T[K] extends object ? DeepReadonly : T[K]; +}; + + +// Usage: +type A = { + name: string; + details: { + age: number; + address: { city: string; zip: string }; + }; +}; + +type ReadonlyA = DeepReadonly; +/* +Type ReadonlyA: +{ + readonly name: string; + readonly details: { + readonly age: number; + readonly address: { readonly city: string; readonly zip: string }; + }; +} +*/ +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/deep-required-type.md b/snippets/typescript/helper-types/deep-required-type.md new file mode 100644 index 00000000..20836483 --- /dev/null +++ b/snippets/typescript/helper-types/deep-required-type.md @@ -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 extends object + ? { [K in keyof T]-?: DeepRequired } + : T; + + +// Usage: +type A = { + id?: string; + name?: string; + details?: { + age?: number; + address?: { city?: string; zip?: string }; + }; +}; + +type RequiredA = DeepRequired; +// Result: { id: string; name: string; details: { age: number; address: { city: string; zip: string }; }; } +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/keys-of-type.md b/snippets/typescript/helper-types/keys-of-type.md new file mode 100644 index 00000000..ddc2a84b --- /dev/null +++ b/snippets/typescript/helper-types/keys-of-type.md @@ -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 = { [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; // "name" +type BooleanKeys = KeysOfType; // "isActive" | "isDeleted" +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/keys-to-optional.md b/snippets/typescript/helper-types/keys-to-optional.md new file mode 100644 index 00000000..54c64fb7 --- /dev/null +++ b/snippets/typescript/helper-types/keys-to-optional.md @@ -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 = Omit & Partial>; + + +// Usage: +type A = { + id: string; + name: string; + age: number; +}; + +type WithOptionalName = OptionalKeys; +// { id: string; age: number; name?: string } + +type WithOptionalNameAndAge = OptionalKeys; +// Result: { id: string; name?: string; age?: number } +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/nullable-keys.md b/snippets/typescript/helper-types/nullable-keys.md new file mode 100644 index 00000000..ff8ed161 --- /dev/null +++ b/snippets/typescript/helper-types/nullable-keys.md @@ -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 = { + [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; // "name" | "description" +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/omit-keys-of-type.md b/snippets/typescript/helper-types/omit-keys-of-type.md new file mode 100644 index 00000000..9e08afa7 --- /dev/null +++ b/snippets/typescript/helper-types/omit-keys-of-type.md @@ -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 = { + [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; // { id: string; data: number[] } +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/required-keys.md b/snippets/typescript/helper-types/required-keys.md new file mode 100644 index 00000000..fa96d1b2 --- /dev/null +++ b/snippets/typescript/helper-types/required-keys.md @@ -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 = { + [K in keyof T]-?: {} extends Pick ? never : K; +}[keyof T]; + + +// Usage: +type A = { + id: string; + name?: string; + isActive: boolean; +}; + +type ReqKeys = RequiredKeys; // "id" | "isActive" +``` \ No newline at end of file diff --git a/snippets/typescript/helper-types/union-to-intersection.md b/snippets/typescript/helper-types/union-to-intersection.md new file mode 100644 index 00000000..04cc24b5 --- /dev/null +++ b/snippets/typescript/helper-types/union-to-intersection.md @@ -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 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; +// { id: string } & { name: string } & { age: number } +``` \ No newline at end of file