File: strictSubtypeAndNarrowing.ts

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (232 lines) | stat: -rw-r--r-- 5,434 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// @strict: true

// Check that `any` is a strict supertype of `unknown`

declare const x11: { x: unknown };
declare const x12: { x: any };

const a11 = [x11, x12];
const a12 = [x12, x11];

declare const x21: { x: any };
declare const x22: { x: unknown };

const a21 = [x22, x21];
const a22 = [x21, x22];

// Strict subtype doesn't infer index signatures in non-fresh object types

const x31 = { a: 1 };
declare const x32: { [x: string]: unknown, a: number }

const a31 = [x31, x32];
const a32 = [x32, x31];

declare const x41: { [x: string]: unknown, a: number }
const x42 = { a: 1 };

const a41 = [x42, x41];
const a42 = [x41, x42];

// (...args: A) => R, where A is any, any[], never, or never[] and R is any or unknown, is supertype of all function types.

declare function isFunction<T>(x: unknown): x is T;

type A = (...args: any) => unknown;
type B = (...args: any[]) => unknown;
type C = (...args: never) => unknown;
type D = (...args: never[]) => unknown;

type FnTypes = A | B | C | D;

function fx1(f: (() => void) | undefined) {
    if (isFunction<A>(f)) {
        f;  // () => void
    }
    else {
        f;  // undefined
    }
    f;  // (() => void) | undefined
}

function fx2(f: (() => void) | undefined) {
    if (isFunction<B>(f)) {
        f;  // () => void
    }
    else {
        f;  // undefined
    }
    f;  // (() => void) | undefined
}

function fx3(f: (() => void) | undefined) {
    if (isFunction<C>(f)) {
        f;  // () => void
    }
    else {
        f;  // undefined
    }
    f;  // (() => void) | undefined
}

function fx4(f: (() => void) | undefined) {
    if (isFunction<D>(f)) {
        f;  // () => void
    }
    else {
        f;  // undefined
    }
    f;  // (() => void) | undefined
}

function checkA(f: FnTypes) {
    if (isFunction<A>(f)) {
        f;  // A | B
    }
    else {
        f;  // C | D
    }
    f;  // FnTypes
}

function checkB(f: FnTypes) {
    if (isFunction<B>(f)) {
        f;  // A | B
    }
    else {
        f;  // C | D
    }
    f;  // FnTypes
}

function checkC(f: FnTypes) {
    if (isFunction<C>(f)) {
        f;  // FnTypes
    }
    else {
        f;  // never
    }
    f;  // FnTypes
}

function checkD(f: FnTypes) {
    if (isFunction<C>(f)) {
        f;  // FnTypes
    }
    else {
        f;  // never
    }
    f;  // FnTypes
}

// Type of x = y is y with freshness preserved

function fx10(obj1: { x?: number }, obj2: { x?: number, y?: number }) {
    obj1 = obj2 = { x: 1, y: 2 };
    obj2 = obj1 = { x: 1, y: 2 };
}

function fx11(): { x?: number } {
    let obj: { x?: number, y?: number };
    return obj = { x: 1, y: 2 };
}

// Repros from #52827

declare function isArrayLike(value: any): value is { length: number };

function ff1(value: { [index: number]: boolean, length: number } | undefined) {
    if (isArrayLike(value)) {
        value;
    } else {
        value;
    }
    value;
}

function ff2(value: { [index: number]: boolean, length: number } | string) {
    if (isArrayLike(value)) {
        value;
    } else {
        value;
    }
    value;
}

function ff3(value: string | string[] | { [index: number]: boolean, length: number } | [number, boolean] | number | { length: string } | { a: string } | null | undefined) {
    if (isArrayLike(value)) {
        value;
    } else {
        value;
    }
    value;
}

// Repro from comment in #52984

type DistributedKeyOf<T> = T extends unknown ? keyof T : never;

type NarrowByKeyValue<ObjT, KeyT extends PropertyKey, ValueT> = ObjT extends unknown
    ? KeyT extends keyof ObjT
        ? ValueT extends ObjT[KeyT]
            ? ObjT & Readonly<Record<KeyT, ValueT>>
            : never
        : never
    : never;

type NarrowByDeepValue<ObjT, DeepPathT, ValueT> = DeepPathT extends readonly [
    infer Head extends DistributedKeyOf<ObjT>,
]
    ? NarrowByKeyValue<ObjT, Head, ValueT>
    : DeepPathT extends readonly [infer Head extends DistributedKeyOf<ObjT>, ...infer Rest]
    ? NarrowByKeyValue<ObjT, Head, NarrowByDeepValue<NonNullable<ObjT[Head]>, Rest, ValueT>>
    : never;


declare function doesValueAtDeepPathSatisfy<
    ObjT extends object,
    const DeepPathT extends ReadonlyArray<number | string>,
    ValueT,
>(
    obj: ObjT,
    deepPath: DeepPathT,
    predicate: (arg: unknown) => arg is ValueT,
): obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>;


type Foo = {value: {type: 'A'}; a?: number} | {value: {type: 'B'}; b?: number};

declare function isA(arg: unknown): arg is 'A';
declare function isB(arg: unknown): arg is 'B';

declare function assert(condition: boolean): asserts condition;

function test1(foo: Foo): {value: {type: 'A'}; a?: number} {
    assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
    return foo;
}

function test2(foo: Foo): {value: {type: 'A'}; a?: number} {
    assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
    return foo;
}

// Repro from #53063

interface Free {
    premium: false;
}

interface Premium {
    premium: true;
}

type Union = { premium: false } | { premium: true };

declare const checkIsPremium: (a: Union) => a is Union & Premium;

const f = (value: Union) => {
    if (!checkIsPremium(value)) {
        value.premium;
    }
};