Skip to content

Commit

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

* test(IsTuple): Add IsTuple type test

* docs(IsTuple): Add IsTuple type documentation
  • Loading branch information
haejunejung authored Aug 26, 2024
1 parent 194c856 commit e251c3b
Show file tree
Hide file tree
Showing 7 changed files with 105 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 @@ -107,6 +107,7 @@ export default defineConfig({
items: [
{ text: 'IsArray', link: '/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/reference/predicate/IsNever' },
],
},
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default defineConfig({
items: [
{ text: 'IsArray', link: '/ko/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/ko/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/ko/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/ko/reference/predicate/IsNever' },
],
},
Expand Down
33 changes: 33 additions & 0 deletions docs/ko/reference/predicate/IsTuple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# IsTuple\<T>

## Overview

주어진 타입이 튜플 타입인지 여부를 판별하는 타입이에요.

## Syntax

```ts
type IsTuple<T> = T extends any[] | readonly any[]
? number extends T['length']
? false
: true
: false;
```

- **T**: 검사할 타입이에요.

## Examples

#### Example #1

```ts
type T0 = IsTuple<[]>; // true
type T1 = IsTuple<[number]>; // true
type T2 = IsTuple<[number, string]>; // true

type T3 = IsTuple<any[]>; // false
type T4 = IsTuple<unknown[]>; // false
type T5 = IsTuple<number[]>; // false
type T6 = IsTuple<Array<number>>; // false
type T7 = IsTuple<ReadonlyArray<number>>; // false
```
33 changes: 33 additions & 0 deletions docs/reference/predicate/IsTuple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# IsTuple\<T>

## Overview

A type that determines whether the given type is a tuple type.

## Syntax

```ts
type IsTuple<T> = T extends any[] | readonly any[]
? number extends T['length']
? false
: true
: false;
```

- **T**: The type to check.

## Examples

#### Example #1

```ts
type T0 = IsTuple<[]>; // true
type T1 = IsTuple<[number]>; // true
type T2 = IsTuple<[number, string]>; // true

type T3 = IsTuple<any[]>; // false
type T4 = IsTuple<unknown[]>; // false
type T5 = IsTuple<number[]>; // false
type T6 = IsTuple<Array<number>>; // false
type T7 = IsTuple<ReadonlyArray<number>>; // false
```
19 changes: 19 additions & 0 deletions source/predicate/IsTuple.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @description - A type that determinse whether the given type is a tuple type.
*
* @template T - The type to check.
*
* @returns {Boolean} - `true` if the type is a tuple; otherwise, returns `false`.
*
* @example
* type T0 = IsTuple<[]> // true
* type T1 = IsTuple<[number]> // true
*
* type T2 = IsTuple<any[]> // false
* type T3 = IsTuple<unknown[]> // false
*/
export type IsTuple<T> = T extends any[] | readonly any[]
? number extends T['length']
? false
: true
: false;
1 change: 1 addition & 0 deletions source/predicate/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { IsArray } from './IsArray';
export type { IsEqual } from './IsEqual';
export type { IsTuple } from './IsTuple';
export type { IsNever } from './IsNever';
17 changes: 17 additions & 0 deletions test-d/predicate/IsTuple.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsTuple } from '@/predicate';
import { expectType } from 'tsd';

declare function isTuple<T>(): IsTuple<T>;

// Should be `true`.
expectType<true>(isTuple<[]>());
expectType<true>(isTuple<[number]>());
expectType<true>(isTuple<[number, string]>());
expectType<true>(isTuple<readonly [number]>());

// Should be `false`.
expectType<false>(isTuple<any[]>());
expectType<false>(isTuple<unknown[]>());
expectType<false>(isTuple<number[]>());
expectType<false>(isTuple<Array<number>>());
expectType<false>(isTuple<ReadonlyArray<number>>());

0 comments on commit e251c3b

Please sign in to comment.