Skip to content

Commit

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

* test(Nullish): Add Nullish type test

* docs(Nullish): Add Nullish documentation
  • Loading branch information
haejunejung authored Aug 21, 2024
1 parent 4657ad2 commit b1004fc
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default defineConfig({
text: 'Basic',
items: [
{ text: 'Falsy', link: '/reference/basic/Falsy' },
{ text: 'Nullish', link: '/reference/basic/Nullish' },
{ text: 'Primitive', link: '/reference/basic/Primitive' },
],
},
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default defineConfig({
text: 'Basic',
items: [
{ text: 'Falsy', link: '/ko/reference/basic/Falsy' },
{ text: 'Nullish', link: '/ko/reference/basic/Nullish' },
{ text: 'Primitive', link: '/ko/reference/basic/Primitive' },
],
},
Expand Down
38 changes: 38 additions & 0 deletions docs/ko/reference/basic/Nullish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Nullish

## 개요

`Nullish` 타입은 값이 없거나 변수가 초기화 되지 않았음을 나타내요.

`Nullish`의 목적은 항상 값을 가질 수 없는 선택적 속성, 변수, 혹은 함수의 매개변수를 다루기 위해서 사용돼요. 이는 `null` 혹은 `undefined` 경우를 명시적으로 처리하여 코드의 신뢰성을 향상시키는데 도움을 줄 수 있어요.

## 문법

```ts
type Nullish = null | undefined;
```

## 예제

#### 예제 #1

```ts
type Form = {
email: string | Nullish;
password: string | Nullish;
};
```

#### 예제 #2

```ts
function isNullish(value: unknown): value is Nullish {
return value === null || value === undefined;
}

const value = [1, 2, 3, null, 5, 6, undefined];

if (value.some(isNullish)) {
throw new TypeError('there is a null or undefined type.');
}
```
38 changes: 38 additions & 0 deletions docs/reference/basic/Nullish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Nullish

## Overview

The `Nullish` type indicates the absence of a value or signifies that a variable has not been initialized.

It's purpose is to handle optional properties, variables or function parameters that may no always have a value. It helps enhance the reliability of the code by explicitly handling there `null` or `undefined` cases.

## Syntax

```ts
type Nullish = null | undefined;
```

## Examples

#### Example #1

```ts
type Form = {
email: string | Nullish;
password: string | Nullish;
};
```

#### Example #2

```ts
function isNullish(value: unknown): value is Nullish {
return value === null || value === undefined;
}

const value = [1, 2, 3, null, 5, 6, undefined];

if (value.some(isNullish)) {
throw new TypeError('there is a null or undefined type.');
}
```
14 changes: 14 additions & 0 deletions source/basic/Nullish.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @description
* The Nullish type indicates the absence of a value or signifies that a variable has not been initalized.
*
* It's purpose is to handle optional properties, variables or function paramters
* that may no always have a value. It helps enhance the reliability of the code by
* explicitly handling there `null` or `undefined` cases.
*
* The name `Nullish` came from ECMAScript.
* @see https://tc39.es/ecma262/#:~:text=%E2%80%9Cnullish%E2%80%9D%20values%20(null%20or%20undefined)
*
*/

export type Nullish = null | undefined;
1 change: 1 addition & 0 deletions source/basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { Falsy } from './Falsy';
export type { Nullish } from './Nullish';
export type { Primitive } from './Primitive';
26 changes: 26 additions & 0 deletions test-d/basic/Nullish.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import { Nullish } from '@/basic';

// Valid cases where `Nullish` should be assignable
expectAssignable<Nullish>(null);
expectAssignable<Nullish>(undefined);

// Cases wherer `Nullish` should not be assignable
expectNotAssignable<Nullish>('');
expectNotAssignable<Nullish>(true);
expectNotAssignable<Nullish>(false);
expectNotAssignable<Nullish>('string');
expectNotAssignable<Nullish>(0);
expectNotAssignable<Nullish>(1);
expectNotAssignable<Nullish>(0n);
expectNotAssignable<Nullish>(Symbol('a'));
expectNotAssignable<Nullish>({});
expectNotAssignable<Nullish>([]);
expectNotAssignable<Nullish>(new Date());
expectNotAssignable<Nullish>(function () {});
expectNotAssignable<Nullish>(/regex/);
expectNotAssignable<Nullish>(new Map());
expectNotAssignable<Nullish>(new Set());
expectNotAssignable<Nullish>(Promise.resolve());
expectNotAssignable<Nullish>(new WeakMap());
expectNotAssignable<Nullish>(new WeakSet());

0 comments on commit b1004fc

Please sign in to comment.