-
-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathoptional.ts
More file actions
46 lines (37 loc) · 1.7 KB
/
optional.ts
File metadata and controls
46 lines (37 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {expectType} from 'tsd';
import type {Optional} from '../index.d.ts';
// Basic
expectType<string | undefined>({} as Optional<string>);
expectType<{foo: string} | undefined>({} as Optional<{foo: string}>);
expectType<'foo' | undefined>({} as Optional<'foo'>);
expectType<42 | undefined>({} as Optional<42>);
expectType<boolean | undefined>({} as Optional<boolean>);
expectType<string | number | undefined>({} as Optional<string | number>);
expectType<(() => void) | undefined>({} as Optional<() => void>);
// Strips null
expectType<string | undefined>({} as Optional<string | null>);
expectType<string | undefined>({} as Optional<string | null | undefined>);
expectType<number | boolean | undefined>({} as Optional<number | null | boolean>);
expectType<true | undefined>({} as Optional<true | null>);
// Already undefined (idempotent)
expectType<string | undefined>({} as Optional<string | undefined>);
// Pure null becomes undefined (null is stripped, undefined remains)
declare const pureNull: Optional<null>;
expectType<undefined>(pureNull);
// Null | undefined becomes undefined
declare const nullOrUndefined: Optional<null | undefined>;
expectType<undefined>(nullOrUndefined);
// Pure undefined stays undefined
declare const pureUndefined: Optional<undefined>;
expectType<undefined>(pureUndefined);
// Nested Optional is idempotent
expectType<string | undefined>({} as Optional<Optional<string>>);
// Void
declare const voidOptional: Optional<void>;
expectType<void | undefined>(voidOptional);
// Edge cases
expectType<any>({} as Optional<any>);
declare const neverOptional: Optional<never>;
expectType<undefined>(neverOptional);
// `unknown | undefined` simplifies to `unknown`
expectType<unknown>({} as Optional<unknown>);