-
-
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.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
5a19e38
commit 6116ec2
Showing
5 changed files
with
183 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* @file Type Tests - Times | ||
* @module tutils/types/tests/unit-d/Times | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/vehicle' | ||
import type EmptyArray from '../empty-array' | ||
import type TestSubject from '../times' | ||
|
||
describe('unit-d:types/Times', () => { | ||
type V = Vehicle | Vehicle['vin'] | Vehicle['year'] | ||
|
||
it('should equal EmptyArray if L is never', () => { | ||
expectTypeOf<TestSubject<never, V>>().toEqualTypeOf<EmptyArray>() | ||
}) | ||
|
||
it('should equal V[] if L is any', () => { | ||
expectTypeOf<TestSubject<any, V>>().toEqualTypeOf<V[]>() | ||
}) | ||
|
||
describe('L extends number', () => { | ||
it('should construct tuple of length L', () => { | ||
expectTypeOf<TestSubject<100, V>>().toEqualTypeOf< | ||
[ | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V, | ||
V | ||
] | ||
>() | ||
}) | ||
|
||
it('should equal V[] if number extends L', () => { | ||
expectTypeOf<TestSubject<number, V>>().toEqualTypeOf<V[]>() | ||
}) | ||
}) | ||
}) |
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,44 @@ | ||
/** | ||
* @file Type Definitions - Times | ||
* @module tutils/types/Times | ||
*/ | ||
|
||
import type EmptyArray from './empty-array' | ||
import type IsNever from './is-never' | ||
|
||
/** | ||
* Construct a tuple of length `L`. | ||
* | ||
* @example | ||
* type X = Times<5> | ||
* // [unknown, unknown, unknown, unknown, unknown] | ||
* @example | ||
* type X = Times<5, number> | ||
* // [number, number, number, number, number] | ||
* @example | ||
* type X = Times<number, number> | ||
* // number[] | ||
* @example | ||
* type X = Times<any, number> | ||
* // number[] | ||
* @example | ||
* type X = Times<never, number> | ||
* // [] | ||
* | ||
* @template L - Tuple length | ||
* @template V - Tuple item type | ||
* @template Acc - Tuple elements accumulator | ||
*/ | ||
type Times< | ||
L extends number, | ||
V = unknown, | ||
Acc extends readonly unknown[] = EmptyArray | ||
> = IsNever<L> extends true | ||
? EmptyArray | ||
: number extends L | ||
? V[] | ||
: Acc extends { length: L } | ||
? Acc | ||
: Times<L, V, [...Acc, V]> | ||
|
||
export type { Times as default } |