-
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(Nullish): Add Nullish type * test(Nullish): Add Nullish type test * docs(Nullish): Add Nullish documentation
- Loading branch information
1 parent
4657ad2
commit b1004fc
Showing
7 changed files
with
119 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,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.'); | ||
} | ||
``` |
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,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.'); | ||
} | ||
``` |
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,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; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export type { Falsy } from './Falsy'; | ||
export type { Nullish } from './Nullish'; | ||
export type { Primitive } from './Primitive'; |
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 { 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()); |