-
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(NonEmptyArray): Add
NonEmptyArray
(#66)
* feat(NonEmptyArray): Add NonEmptyArray type * test(NonEmptyArray): Add NonEmptyArray test * docs(NonEmptyArray): Add NonEmptyArray docs
- Loading branch information
1 parent
3eb52bf
commit fc7b2b6
Showing
7 changed files
with
101 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,27 @@ | ||
# NonEmptyArray\<T> | ||
|
||
## 개요 | ||
|
||
T 타입의 요소를 적어도 하나를 포함하는 배열을 나타내요. | ||
|
||
## 문법 | ||
|
||
```ts | ||
type NonEmptyArray<T = unknown> = [T, ...T[]]; | ||
``` | ||
|
||
- **T**: 배열에 포함된 요소의 타입이에요. | ||
|
||
## 예제 | ||
|
||
```ts | ||
// Using NonEmptyArray with a string type: | ||
type stringArray = NonEmptyArray<string> = ['1', '2', '3']; | ||
|
||
// Using NonEmptyArray with a number type: | ||
type numberArray = NonEmptyArray<number> = [1, 2, 3]; | ||
|
||
// Invalid example (empty array not allowed): | ||
// Error: Type '[]' is not assignable to the type 'NonEmptyArray<number>'. | ||
const emptyArray: NonEmptyArray<number> = []; | ||
``` |
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,27 @@ | ||
# NonEmptyArray\<T> | ||
|
||
## Overview | ||
|
||
Represents an array that is guaranteed to contain at least one element of type T. | ||
|
||
## Syntax | ||
|
||
```ts | ||
type NonEmptyArray<T = unknown> = [T, ...T[]]; | ||
``` | ||
|
||
- **T**: The type of elements contained in the array. | ||
|
||
## Example | ||
|
||
```ts | ||
// Using NonEmptyArray with a string type: | ||
type stringArray = NonEmptyArray<string> = ['1', '2', '3']; | ||
|
||
// Using NonEmptyArray with a number type: | ||
type numberArray = NonEmptyArray<number> = [1, 2, 3]; | ||
|
||
// Invalid example (empty array not allowed): | ||
// Error: Type '[]' is not assignable to the type 'NonEmptyArray<number>'. | ||
const emptyArray: NonEmptyArray<number> = []; | ||
``` |
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,20 @@ | ||
/** | ||
* @description - Represents an array that is guaranteed to contain at least one element of type T. | ||
* | ||
* @template T - The type of elements contained in the array. | ||
* | ||
* @returns - A tuple where the first element is of type T. | ||
* | ||
* @example | ||
* // Using NonEmptyArray with a string type: | ||
* type stringArray = NonEmptyArray<string> = ['1', '2', '3']; | ||
* | ||
* // Using NonEmptyArray with a number type: | ||
* type numberArray = NonEmptyArray<number> = [1, 2, 3]; | ||
* | ||
* // Invalid example (empty array not allowed): | ||
* // Error: Type '[]' is not assignable to the type 'NonEmptyArray<number>'. | ||
* const emptyArray: NonEmptyArray<number> = []; | ||
* | ||
*/ | ||
export type NonEmptyArray<T = unknown> = [T, ...T[]]; |
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 @@ | ||
export type { NonEmptyArray } from './NonEmptyArray'; |
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,11 @@ | ||
import { NonEmptyArray } from '@/array'; | ||
import { expectError, expectNotAssignable, expectType } from 'tsd'; | ||
|
||
declare const sum: (...nums: NonEmptyArray<number>) => number; | ||
|
||
// Should correctly handle non-empty array of numbers; | ||
expectType<number>(sum(1, 2, 3)); | ||
expectType<number>(sum(1)); | ||
|
||
// Should not be assignable to an empty array. | ||
expectNotAssignable<NonEmptyArray<number>>([]); |