Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PickRequired): Add PickRequired #56

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
37 changes: 37 additions & 0 deletions docs/ko/reference/utilities/PickRequired.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PickRequired\<T, K>

## 개요

선택된 키를 필수 사항으로 만들고 나머지 타입은 변경되지 않은 상태를 유지하는 새로운 타입을 생성해요.

## 문법

```ts
type PickRequired<T, K extends keyof T> = Simplify<
Omit<T, K> & Required<Pick<T, K>>
>;
```

- **T** - 키를 선택할 원본 타입이에요.
- **K** - 원본 타입 `T`에서 필수 사항으로 만들 키예요.

## 예제

#### 예제 #1

```ts
type Post = {
id: number;
authorId: number;
role: 'seller' | 'buyer';
price?: number;
};

type PriceRequiredPost = PickRequired<Post, 'price'>;
// {
// id: number;
// authorId: number;
// role: 'seller' | 'buyer';
// price: number;
// }
```
37 changes: 37 additions & 0 deletions docs/reference/utilities/PickRequired.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PickRequired\<T, K>

## 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<T, K extends keyof T> = Simplify<
Omit<T, K> & Required<Pick<T, K>>
>;
```

- **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<Post, 'price'>;
// {
// id: number;
// authorId: number;
// role: 'seller' | 'buyer';
// price: number;
// }
```
31 changes: 31 additions & 0 deletions source/utilities/PickRequired.d.ts
Original file line number Diff line number Diff line change
@@ -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<Post, 'price'>;
* // {
* // id: number;
* // authorId: number;
* // role: 'seller' | 'buyer';
* // price: number;
* // }
*/
export type PickRequired<T, K extends keyof T> = Simplify<
Omit<T, K> & Required<Pick<T, K>>
>;
1 change: 1 addition & 0 deletions source/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
43 changes: 43 additions & 0 deletions test-d/utilities/PickRequired.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { PickRequired } from '@/utilities';
import { expectType } from 'tsd';

declare function pickRequired<T, K extends keyof T>(): PickRequired<T, K>;

// 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<PriceRequiredPost>(pickRequired<Post, 'price'>());

// 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<Expected>(pickRequired<TA & TB, 'b' | 'c' | 'd'>());
Loading