Skip to content

Commit

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

* test(NonEmptyArray): Add NonEmptyArray test

* docs(NonEmptyArray): Add NonEmptyArray docs
  • Loading branch information
haejunejung authored Sep 3, 2024
1 parent 3eb52bf commit fc7b2b6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default defineConfig({
{ text: 'Uppercase', link: '/reference/built-in/Uppercase' },
],
},
{
text: 'Array',
items: [
{ text: 'NonEmptyArray', link: '/reference/array/NonEmptyArray' },
],
},
{
text: 'Basic',
items: [
Expand Down
9 changes: 9 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export default defineConfig({
{ text: 'Uppercase', link: '/ko/reference/built-in/Uppercase' },
],
},
{
text: 'Array',
items: [
{
text: 'NonEmptyArray',
link: '/ko/reference/array/NonEmptyArray',
},
],
},
{
text: 'Basic',
items: [
Expand Down
27 changes: 27 additions & 0 deletions docs/ko/reference/array/NonEmptyArray.md
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> = [];
```
27 changes: 27 additions & 0 deletions docs/reference/array/NonEmptyArray.md
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> = [];
```
20 changes: 20 additions & 0 deletions source/array/NonEmptyArray.d.ts
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[]];
1 change: 1 addition & 0 deletions source/array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { NonEmptyArray } from './NonEmptyArray';
11 changes: 11 additions & 0 deletions test-d/array/NonEmptyArray.test-d.ts
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>>([]);

0 comments on commit fc7b2b6

Please sign in to comment.