diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index 69d744c..2a5c837 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -53,6 +53,10 @@ export default defineConfig({ text: 'PickOptional', link: '/reference/utilities/PickOptional', }, + { + text: 'PickRequired', + link: '/reference/utilities/PickRequired', + }, { text: 'Simplify', link: '/reference/utilities/Simplify', diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index 61ab5df..8a35ebb 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -56,6 +56,10 @@ export default defineConfig({ text: 'PickOptional', link: '/ko/reference/utilities/PickOptional', }, + { + text: 'PickRequired', + link: '/ko/reference/utilities/PickRequired', + }, { text: 'Simplify', link: '/ko/reference/utilities/Simplify', diff --git a/docs/ko/reference/utilities/PickRequired.md b/docs/ko/reference/utilities/PickRequired.md new file mode 100644 index 0000000..83aa97d --- /dev/null +++ b/docs/ko/reference/utilities/PickRequired.md @@ -0,0 +1,37 @@ +# PickRequired\ + +## 개요 + +선택된 키를 필수 사항으로 만들고 나머지 타입은 변경되지 않은 상태를 유지하는 새로운 타입을 생성해요. + +## 문법 + +```ts +type PickRequired = Simplify< + Omit & Required> +>; +``` + +- **T** - 키를 선택할 원본 타입이에요. +- **K** - 원본 타입 `T`에서 필수 사항으로 만들 키예요. + +## 예제 + +#### 예제 #1 + +```ts +type Post = { + id: number; + authorId: number; + role: 'seller' | 'buyer'; + price?: number; +}; + +type PriceRequiredPost = PickRequired; +// { +// id: number; +// authorId: number; +// role: 'seller' | 'buyer'; +// price: number; +// } +``` diff --git a/docs/reference/utilities/PickRequired.md b/docs/reference/utilities/PickRequired.md new file mode 100644 index 0000000..50acdd1 --- /dev/null +++ b/docs/reference/utilities/PickRequired.md @@ -0,0 +1,37 @@ +# PickRequired\ + +## Overview + +Creates a new type where the specified keys are made required, while keeping the rest of the type remain unchanged. + +## Syntax + +```ts +type PickRequired = Simplify< + Omit & Required> +>; +``` + +- **T** - The original type from which keys are being picked. +- **K** - The keys from the original type `T` that should be made required. + +## Examples + +#### Example #1 + +```ts +type Post = { + id: number; + authorId: number; + role: 'seller' | 'buyer'; + price?: number; +}; + +type PriceRequiredPost = PickRequired; +// { +// id: number; +// authorId: number; +// role: 'seller' | 'buyer'; +// price: number; +// } +``` diff --git a/source/utilities/PickRequired.d.ts b/source/utilities/PickRequired.d.ts new file mode 100644 index 0000000..1a61c6e --- /dev/null +++ b/source/utilities/PickRequired.d.ts @@ -0,0 +1,31 @@ +import { Simplify } from './Simplify'; + +/** + * @description + * Creates a new type where the specified keys are made required, + * while keeping the rest of the type remain unchanged. + * + * @template T - The original type from which keys are being picked. + * @template K - The keys from the original type `T` that should be made required. + * + * @returns - A new type with keys in K made required. + * + * @example + * type Post = { + * id: number; + * authorId: number; + * role: 'seller' | 'buyer'; + * price?: number; + * }; + * + * type PriceRequiredPost = PickRequired; + * // { + * // id: number; + * // authorId: number; + * // role: 'seller' | 'buyer'; + * // price: number; + * // } + */ +export type PickRequired = Simplify< + Omit & Required> +>; diff --git a/source/utilities/index.ts b/source/utilities/index.ts index e8891be..a2aa7bb 100644 --- a/source/utilities/index.ts +++ b/source/utilities/index.ts @@ -1,4 +1,5 @@ export type { PickOptional } from './PickOptional'; +export type { PickRequired } from './PickRequired'; export type { Simplify } from './Simplify'; export type { StrictExclude } from './StrictExclude'; export type { StrictExtract } from './StrictExtract'; diff --git a/test-d/utilities/PickRequired.test-d.ts b/test-d/utilities/PickRequired.test-d.ts new file mode 100644 index 0000000..e432804 --- /dev/null +++ b/test-d/utilities/PickRequired.test-d.ts @@ -0,0 +1,43 @@ +import { PickRequired } from '@/utilities'; +import { expectType } from 'tsd'; + +declare function pickRequired(): PickRequired; + +// Should correctly creates a new type where the specified keys are made required. +type Post = { + id: number; + authorId: number; + role: 'seller' | 'buyer'; + price?: number; +}; + +type PriceRequiredPost = { + id: number; + authorId: number; + role: 'seller' | 'buyer'; + price: number; +}; + +expectType(pickRequired()); + +// Should correctly handle union type. +type TA = { + a: number; + b?: string; + c?: boolean; +}; + +type TB = { + d?: null; + e: '0'; +}; + +type Expected = { + a: number; + b: string; + c: boolean; + d: null; + e: '0'; +}; + +expectType(pickRequired());