Skip to content

Commit

Permalink
feat(PickNonNullable): Add PickNonNullable (#63)
Browse files Browse the repository at this point in the history
* feat(PickNonNullable): Add PickNonNullable type

* test(PickNonNullable): Add PickNonNullable test

* docs(PickNonNullable): Add PickNonNullable docs
  • Loading branch information
haejunejung authored Sep 2, 2024
1 parent 8ad37a1 commit 7f0eefa
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export default defineConfig({
{
text: 'Utilities',
items: [
{
text: 'PickNonNullable',
link: '/reference/utilities/PickNonNullable',
},
{
text: 'PickOptional',
link: '/reference/utilities/PickOptional',
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export default defineConfig({
{
text: 'Utilities',
items: [
{
text: 'PickNonNullable',
link: '/ko/reference/utilities/PickNonNullable',
},
{
text: 'PickOptional',
link: '/ko/reference/utilities/PickOptional',
Expand Down
33 changes: 33 additions & 0 deletions docs/ko/reference/utilities/PickNonNullable.md
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;
// }
```
33 changes: 33 additions & 0 deletions docs/reference/utilities/PickNonNullable.md
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;
// }
```
29 changes: 29 additions & 0 deletions source/utilities/PickNonNullable.d.ts
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]> }
>;
1 change: 1 addition & 0 deletions source/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { PickNonNullable } from './PickNonNullable';
export type { PickOptional } from './PickOptional';
export type { PickReadonly } from './PickReadonly';
export type { PickRequired } from './PickRequired';
Expand Down
26 changes: 26 additions & 0 deletions test-d/utilities/PickNonNullable.test-d.ts
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'>());

0 comments on commit 7f0eefa

Please sign in to comment.