-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bcad4c
commit 768a88b
Showing
1 changed file
with
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expectType } from 'tsd'; | ||
import { init } from '../es'; | ||
|
||
// string always return string | ||
expectType<string>(init('abc')); | ||
// emptyString still returns type string. this is due to `''.chartAt(0) => ''` | ||
expectType<string>(init('')); | ||
|
||
// array literals will read the first type correctly | ||
expectType<[string, number]>(init(['fi', 1, 'fum'])); | ||
// but if the array is typed as an `Array<T> or T[]`, then return type will be `T` | ||
expectType<Array<string | number>>(init(['fi', 1, 'fum'] as Array<string | number>)); | ||
// empty array literals return never | ||
expectType<never>(init([])); | ||
// but if it is typed, it will be `number[]` | ||
expectType<number[]>(init([] as number[])); | ||
// single entry tuples return never, since they literally have no init | ||
expectType<never>(init([10] as const)); | ||
// tuples return the example type of the input tuple minus the first entry | ||
expectType<[10, '10']>(init([10, '10', 10] as const)); | ||
expectType<['10', 10]>(init(['10', 10, '10'] as const)); | ||
// typed arrays return the same type | ||
expectType<Array<number | string>>(init([10, 'ten'] as Array<number | string>)); | ||
expectType<Array<string | number>>(init(['10', 10] as Array<string | number>)); |