diff --git a/test/init.test.ts b/test/init.test.ts new file mode 100644 index 0000000..ee83ba4 --- /dev/null +++ b/test/init.test.ts @@ -0,0 +1,24 @@ +import { expectType } from 'tsd'; +import { init } from '../es'; + +// string always return string +expectType(init('abc')); +// emptyString still returns type string. this is due to `''.chartAt(0) => ''` +expectType(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 or T[]`, then return type will be `T` +expectType>(init(['fi', 1, 'fum'] as Array)); +// empty array literals return never +expectType(init([])); +// but if it is typed, it will be `number[]` +expectType(init([] as number[])); +// single entry tuples return never, since they literally have no init +expectType(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>(init([10, 'ten'] as Array)); +expectType>(init(['10', 10] as Array));