-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PickNonNullable): Add
PickNonNullable
(#63)
* feat(PickNonNullable): Add PickNonNullable type * test(PickNonNullable): Add PickNonNullable test * docs(PickNonNullable): Add PickNonNullable docs
- Loading branch information
1 parent
8ad37a1
commit 7f0eefa
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# PickNonNullable\<T, K> | ||
|
||
## 개요 | ||
|
||
선택된 키에 대해 `null`과 `undefined`를 제거하여 `NonNullable`로 변환하고, 나머지 타입은 변경되지 않은 상태를 유지하는 새로운 타입을 생성해요. | ||
|
||
## 문법 | ||
|
||
```ts | ||
type PickNonNullable<T, K extends keyof T> = Simplify< | ||
Omit<T, K> & { [P in K]: NonNullable<T[P]> } | ||
>; | ||
``` | ||
|
||
- **T** - 키를 선택할 원본 타입이에요. | ||
- **K** - 원본 타입 `T`에서 `NonNullable`을 적용할 속성의 키예요. | ||
|
||
## 예제 | ||
|
||
```ts | ||
type SomeType = { | ||
readonly a: number | null | undefined; | ||
b: boolean; | ||
c: string; | ||
}; | ||
|
||
type SomeNonNullableType = PickNonNullable<SomeType, 'a'>; | ||
// { | ||
// readonly a: number; | ||
// b: boolean; | ||
// c: string; | ||
// } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# PickNonNullable\<T, K> | ||
|
||
## Overview | ||
|
||
Creates a new type by converting the selected keys to NonNullable to remove null and undefined, while keeping the rest of the type unchanged. | ||
|
||
## Syntax | ||
|
||
```ts | ||
type PickNonNullable<T, K extends keyof T> = Simplify< | ||
Omit<T, K> & { [P in K]: NonNullable<T[P]> } | ||
>; | ||
``` | ||
|
||
- **T** - The original type from which keys are being picked. | ||
- **K** - The keys from the original type `T` that should be made non-nullable. | ||
|
||
## Example | ||
|
||
```ts | ||
type SomeType = { | ||
readonly a: number | null | undefined; | ||
b: boolean; | ||
c: string; | ||
}; | ||
|
||
type SomeNonNullableType = PickNonNullable<SomeType, 'a'>; | ||
// { | ||
// readonly a: number; | ||
// b: boolean; | ||
// c: string; | ||
// } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Simplify } from './Simplify'; | ||
|
||
/** | ||
* @description | ||
* Creates a new type by converting the selected keys to NonNullable to remove null and undefined, | ||
* while keeping the rest of the type unchanged. | ||
* | ||
* @template T - The original type from which keys are being picked. | ||
* @template K - The keys of the `T` to which NonNullable will be applied. | ||
* | ||
* @returns - A new type with keys in K made non-nullable. | ||
* | ||
* @example | ||
* type SomeType = { | ||
* readonly a: number | null | undefined; | ||
* b: boolean; | ||
* c: string; | ||
* } | ||
* | ||
* type SomeNonNullableType = PickNonNullable<SomeType, 'a'>; | ||
* // { | ||
* // readonly a: number; | ||
* // b: boolean; | ||
* // c: string; | ||
* // } | ||
*/ | ||
export type PickNonNullable<T, K extends keyof T> = Simplify< | ||
Omit<T, K> & { [P in K]: NonNullable<T[P]> } | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PickNonNullable } from '@/utilities'; | ||
import { expectNotType, expectType } from 'tsd'; | ||
|
||
declare function pickNonNullable<T, K extends keyof T>(): PickNonNullable<T, K>; | ||
|
||
type SomeType = { | ||
readonly a: number | null | undefined; | ||
b: boolean; | ||
c: string; | ||
}; | ||
|
||
type SomeTypeWithANonNullable = { | ||
readonly a: number; | ||
b: boolean; | ||
c: string; | ||
}; | ||
|
||
// Should be `SomeTypeWithANonNullable`, because 'a' is the only property being made non-nullable. | ||
expectType<SomeTypeWithANonNullable>(pickNonNullable<SomeType, 'a'>()); | ||
|
||
// Should not be `SomeTypeWithANonNullable` because 'b' was not made non-nullable. | ||
expectNotType<SomeTypeWithANonNullable>(pickNonNullable<SomeType, 'b'>()); | ||
|
||
// Should be `SomeTypeWithANonNullable` when 'a' and 'b' are boht made non-nullable. | ||
// Since 'b' is not nullable to begin with, it remains unchanged. | ||
expectType<SomeTypeWithANonNullable>(pickNonNullable<SomeType, 'a' | 'b'>()); |