|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {Optional} from '../index.d.ts'; |
| 3 | + |
| 4 | +// Basic |
| 5 | +expectType<string | undefined>({} as Optional<string>); |
| 6 | +expectType<{foo: string} | undefined>({} as Optional<{foo: string}>); |
| 7 | +expectType<'foo' | undefined>({} as Optional<'foo'>); |
| 8 | +expectType<42 | undefined>({} as Optional<42>); |
| 9 | +expectType<boolean | undefined>({} as Optional<boolean>); |
| 10 | +expectType<string | number | undefined>({} as Optional<string | number>); |
| 11 | +expectType<(() => void) | undefined>({} as Optional<() => void>); |
| 12 | + |
| 13 | +// Strips `null` |
| 14 | +expectType<string | undefined>({} as Optional<string | null>); |
| 15 | +expectType<string | undefined>({} as Optional<string | null | undefined>); |
| 16 | +expectType<number | boolean | undefined>({} as Optional<number | null | boolean>); |
| 17 | +expectType<true | undefined>({} as Optional<true | null>); |
| 18 | + |
| 19 | +// Already `undefined` (idempotent) |
| 20 | +expectType<string | undefined>({} as Optional<string | undefined>); |
| 21 | + |
| 22 | +// Pure `null` becomes `undefined` (`null` is stripped, `undefined` remains) |
| 23 | +declare const pureNull: Optional<null>; |
| 24 | +expectType<undefined>(pureNull); |
| 25 | + |
| 26 | +// `null | undefined` becomes `undefined` |
| 27 | +declare const nullOrUndefined: Optional<null | undefined>; |
| 28 | +expectType<undefined>(nullOrUndefined); |
| 29 | + |
| 30 | +// Pure `undefined` stays `undefined` |
| 31 | +declare const pureUndefined: Optional<undefined>; |
| 32 | +expectType<undefined>(pureUndefined); |
| 33 | + |
| 34 | +// Nested `Optional` is idempotent |
| 35 | +expectType<string | undefined>({} as Optional<Optional<string>>); |
| 36 | + |
| 37 | +// `void` |
| 38 | +declare const voidOptional: Optional<void>; |
| 39 | +expectType<void | undefined>(voidOptional); |
| 40 | + |
| 41 | +// Edge cases |
| 42 | +expectType<any>({} as Optional<any>); |
| 43 | +declare const neverOptional: Optional<never>; |
| 44 | +expectType<undefined>(neverOptional); |
| 45 | +// `unknown | undefined` simplifies to `unknown` |
| 46 | +expectType<unknown>({} as Optional<unknown>); |
0 commit comments