Skip to content

Commit 9ea8a55

Browse files
committed
Accept new baselines
1 parent 9b2d602 commit 9ea8a55

5 files changed

+439
-0
lines changed

tests/baselines/reference/strictSubtypeAndNarrowing.errors.txt

+49
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,53 @@ tests/cases/compiler/strictSubtypeAndNarrowing.ts(129,26): error TS2322: Type '{
177177
}
178178
value;
179179
}
180+
181+
// Repro from comment in #52984
182+
183+
type DistributedKeyOf<T> = T extends unknown ? keyof T : never;
184+
185+
type NarrowByKeyValue<ObjT, KeyT extends PropertyKey, ValueT> = ObjT extends unknown
186+
? KeyT extends keyof ObjT
187+
? ValueT extends ObjT[KeyT]
188+
? ObjT & Readonly<Record<KeyT, ValueT>>
189+
: never
190+
: never
191+
: never;
192+
193+
type NarrowByDeepValue<ObjT, DeepPathT, ValueT> = DeepPathT extends readonly [
194+
infer Head extends DistributedKeyOf<ObjT>,
195+
]
196+
? NarrowByKeyValue<ObjT, Head, ValueT>
197+
: DeepPathT extends readonly [infer Head extends DistributedKeyOf<ObjT>, ...infer Rest]
198+
? NarrowByKeyValue<ObjT, Head, NarrowByDeepValue<NonNullable<ObjT[Head]>, Rest, ValueT>>
199+
: never;
200+
201+
202+
declare function doesValueAtDeepPathSatisfy<
203+
ObjT extends object,
204+
const DeepPathT extends ReadonlyArray<number | string>,
205+
ValueT,
206+
>(
207+
obj: ObjT,
208+
deepPath: DeepPathT,
209+
predicate: (arg: unknown) => arg is ValueT,
210+
): obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>;
211+
212+
213+
type Foo = {value: {type: 'A'}; a?: number} | {value: {type: 'B'}; b?: number};
214+
215+
declare function isA(arg: unknown): arg is 'A';
216+
declare function isB(arg: unknown): arg is 'B';
217+
218+
declare function assert(condition: boolean): asserts condition;
219+
220+
function test1(foo: Foo): {value: {type: 'A'}; a?: number} {
221+
assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
222+
return foo;
223+
}
224+
225+
function test2(foo: Foo): {value: {type: 'A'}; a?: number} {
226+
assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
227+
return foo;
228+
}
180229

tests/baselines/reference/strictSubtypeAndNarrowing.js

+57
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,55 @@ function ff3(value: string | string[] | { [index: number]: boolean, length: numb
160160
}
161161
value;
162162
}
163+
164+
// Repro from comment in #52984
165+
166+
type DistributedKeyOf<T> = T extends unknown ? keyof T : never;
167+
168+
type NarrowByKeyValue<ObjT, KeyT extends PropertyKey, ValueT> = ObjT extends unknown
169+
? KeyT extends keyof ObjT
170+
? ValueT extends ObjT[KeyT]
171+
? ObjT & Readonly<Record<KeyT, ValueT>>
172+
: never
173+
: never
174+
: never;
175+
176+
type NarrowByDeepValue<ObjT, DeepPathT, ValueT> = DeepPathT extends readonly [
177+
infer Head extends DistributedKeyOf<ObjT>,
178+
]
179+
? NarrowByKeyValue<ObjT, Head, ValueT>
180+
: DeepPathT extends readonly [infer Head extends DistributedKeyOf<ObjT>, ...infer Rest]
181+
? NarrowByKeyValue<ObjT, Head, NarrowByDeepValue<NonNullable<ObjT[Head]>, Rest, ValueT>>
182+
: never;
183+
184+
185+
declare function doesValueAtDeepPathSatisfy<
186+
ObjT extends object,
187+
const DeepPathT extends ReadonlyArray<number | string>,
188+
ValueT,
189+
>(
190+
obj: ObjT,
191+
deepPath: DeepPathT,
192+
predicate: (arg: unknown) => arg is ValueT,
193+
): obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>;
194+
195+
196+
type Foo = {value: {type: 'A'}; a?: number} | {value: {type: 'B'}; b?: number};
197+
198+
declare function isA(arg: unknown): arg is 'A';
199+
declare function isB(arg: unknown): arg is 'B';
200+
201+
declare function assert(condition: boolean): asserts condition;
202+
203+
function test1(foo: Foo): {value: {type: 'A'}; a?: number} {
204+
assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
205+
return foo;
206+
}
207+
208+
function test2(foo: Foo): {value: {type: 'A'}; a?: number} {
209+
assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
210+
return foo;
211+
}
163212

164213

165214
//// [strictSubtypeAndNarrowing.js]
@@ -284,3 +333,11 @@ function ff3(value) {
284333
}
285334
value;
286335
}
336+
function test1(foo) {
337+
assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
338+
return foo;
339+
}
340+
function test2(foo) {
341+
assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
342+
return foo;
343+
}

tests/baselines/reference/strictSubtypeAndNarrowing.symbols

+172
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,175 @@ function ff3(value: string | string[] | { [index: number]: boolean, length: numb
384384
>value : Symbol(value, Decl(strictSubtypeAndNarrowing.ts, 153, 13))
385385
}
386386

387+
// Repro from comment in #52984
388+
389+
type DistributedKeyOf<T> = T extends unknown ? keyof T : never;
390+
>DistributedKeyOf : Symbol(DistributedKeyOf, Decl(strictSubtypeAndNarrowing.ts, 160, 1))
391+
>T : Symbol(T, Decl(strictSubtypeAndNarrowing.ts, 164, 22))
392+
>T : Symbol(T, Decl(strictSubtypeAndNarrowing.ts, 164, 22))
393+
>T : Symbol(T, Decl(strictSubtypeAndNarrowing.ts, 164, 22))
394+
395+
type NarrowByKeyValue<ObjT, KeyT extends PropertyKey, ValueT> = ObjT extends unknown
396+
>NarrowByKeyValue : Symbol(NarrowByKeyValue, Decl(strictSubtypeAndNarrowing.ts, 164, 63))
397+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 166, 22))
398+
>KeyT : Symbol(KeyT, Decl(strictSubtypeAndNarrowing.ts, 166, 27))
399+
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
400+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 166, 53))
401+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 166, 22))
402+
403+
? KeyT extends keyof ObjT
404+
>KeyT : Symbol(KeyT, Decl(strictSubtypeAndNarrowing.ts, 166, 27))
405+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 166, 22))
406+
407+
? ValueT extends ObjT[KeyT]
408+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 166, 53))
409+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 166, 22))
410+
>KeyT : Symbol(KeyT, Decl(strictSubtypeAndNarrowing.ts, 166, 27))
411+
412+
? ObjT & Readonly<Record<KeyT, ValueT>>
413+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 166, 22))
414+
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
415+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
416+
>KeyT : Symbol(KeyT, Decl(strictSubtypeAndNarrowing.ts, 166, 27))
417+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 166, 53))
418+
419+
: never
420+
: never
421+
: never;
422+
423+
type NarrowByDeepValue<ObjT, DeepPathT, ValueT> = DeepPathT extends readonly [
424+
>NarrowByDeepValue : Symbol(NarrowByDeepValue, Decl(strictSubtypeAndNarrowing.ts, 172, 12))
425+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
426+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 174, 28))
427+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 174, 39))
428+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 174, 28))
429+
430+
infer Head extends DistributedKeyOf<ObjT>,
431+
>Head : Symbol(Head, Decl(strictSubtypeAndNarrowing.ts, 175, 9))
432+
>DistributedKeyOf : Symbol(DistributedKeyOf, Decl(strictSubtypeAndNarrowing.ts, 160, 1))
433+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
434+
435+
]
436+
? NarrowByKeyValue<ObjT, Head, ValueT>
437+
>NarrowByKeyValue : Symbol(NarrowByKeyValue, Decl(strictSubtypeAndNarrowing.ts, 164, 63))
438+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
439+
>Head : Symbol(Head, Decl(strictSubtypeAndNarrowing.ts, 175, 9))
440+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 174, 39))
441+
442+
: DeepPathT extends readonly [infer Head extends DistributedKeyOf<ObjT>, ...infer Rest]
443+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 174, 28))
444+
>Head : Symbol(Head, Decl(strictSubtypeAndNarrowing.ts, 178, 39))
445+
>DistributedKeyOf : Symbol(DistributedKeyOf, Decl(strictSubtypeAndNarrowing.ts, 160, 1))
446+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
447+
>Rest : Symbol(Rest, Decl(strictSubtypeAndNarrowing.ts, 178, 85))
448+
449+
? NarrowByKeyValue<ObjT, Head, NarrowByDeepValue<NonNullable<ObjT[Head]>, Rest, ValueT>>
450+
>NarrowByKeyValue : Symbol(NarrowByKeyValue, Decl(strictSubtypeAndNarrowing.ts, 164, 63))
451+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
452+
>Head : Symbol(Head, Decl(strictSubtypeAndNarrowing.ts, 178, 39))
453+
>NarrowByDeepValue : Symbol(NarrowByDeepValue, Decl(strictSubtypeAndNarrowing.ts, 172, 12))
454+
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
455+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 174, 23))
456+
>Head : Symbol(Head, Decl(strictSubtypeAndNarrowing.ts, 178, 39))
457+
>Rest : Symbol(Rest, Decl(strictSubtypeAndNarrowing.ts, 178, 85))
458+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 174, 39))
459+
460+
: never;
461+
462+
463+
declare function doesValueAtDeepPathSatisfy<
464+
>doesValueAtDeepPathSatisfy : Symbol(doesValueAtDeepPathSatisfy, Decl(strictSubtypeAndNarrowing.ts, 180, 12))
465+
466+
ObjT extends object,
467+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 183, 44))
468+
469+
const DeepPathT extends ReadonlyArray<number | string>,
470+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 184, 24))
471+
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --))
472+
473+
ValueT,
474+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 185, 59))
475+
476+
>(
477+
obj: ObjT,
478+
>obj : Symbol(obj, Decl(strictSubtypeAndNarrowing.ts, 187, 2))
479+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 183, 44))
480+
481+
deepPath: DeepPathT,
482+
>deepPath : Symbol(deepPath, Decl(strictSubtypeAndNarrowing.ts, 188, 14))
483+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 184, 24))
484+
485+
predicate: (arg: unknown) => arg is ValueT,
486+
>predicate : Symbol(predicate, Decl(strictSubtypeAndNarrowing.ts, 189, 24))
487+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 190, 16))
488+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 190, 16))
489+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 185, 59))
490+
491+
): obj is NarrowByDeepValue<ObjT, DeepPathT, ValueT>;
492+
>obj : Symbol(obj, Decl(strictSubtypeAndNarrowing.ts, 187, 2))
493+
>NarrowByDeepValue : Symbol(NarrowByDeepValue, Decl(strictSubtypeAndNarrowing.ts, 172, 12))
494+
>ObjT : Symbol(ObjT, Decl(strictSubtypeAndNarrowing.ts, 183, 44))
495+
>DeepPathT : Symbol(DeepPathT, Decl(strictSubtypeAndNarrowing.ts, 184, 24))
496+
>ValueT : Symbol(ValueT, Decl(strictSubtypeAndNarrowing.ts, 185, 59))
497+
498+
499+
type Foo = {value: {type: 'A'}; a?: number} | {value: {type: 'B'}; b?: number};
500+
>Foo : Symbol(Foo, Decl(strictSubtypeAndNarrowing.ts, 191, 53))
501+
>value : Symbol(value, Decl(strictSubtypeAndNarrowing.ts, 194, 12))
502+
>type : Symbol(type, Decl(strictSubtypeAndNarrowing.ts, 194, 20))
503+
>a : Symbol(a, Decl(strictSubtypeAndNarrowing.ts, 194, 31))
504+
>value : Symbol(value, Decl(strictSubtypeAndNarrowing.ts, 194, 47))
505+
>type : Symbol(type, Decl(strictSubtypeAndNarrowing.ts, 194, 55))
506+
>b : Symbol(b, Decl(strictSubtypeAndNarrowing.ts, 194, 66))
507+
508+
declare function isA(arg: unknown): arg is 'A';
509+
>isA : Symbol(isA, Decl(strictSubtypeAndNarrowing.ts, 194, 79))
510+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 196, 21))
511+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 196, 21))
512+
513+
declare function isB(arg: unknown): arg is 'B';
514+
>isB : Symbol(isB, Decl(strictSubtypeAndNarrowing.ts, 196, 47))
515+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 197, 21))
516+
>arg : Symbol(arg, Decl(strictSubtypeAndNarrowing.ts, 197, 21))
517+
518+
declare function assert(condition: boolean): asserts condition;
519+
>assert : Symbol(assert, Decl(strictSubtypeAndNarrowing.ts, 197, 47))
520+
>condition : Symbol(condition, Decl(strictSubtypeAndNarrowing.ts, 199, 24))
521+
>condition : Symbol(condition, Decl(strictSubtypeAndNarrowing.ts, 199, 24))
522+
523+
function test1(foo: Foo): {value: {type: 'A'}; a?: number} {
524+
>test1 : Symbol(test1, Decl(strictSubtypeAndNarrowing.ts, 199, 63))
525+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 201, 15))
526+
>Foo : Symbol(Foo, Decl(strictSubtypeAndNarrowing.ts, 191, 53))
527+
>value : Symbol(value, Decl(strictSubtypeAndNarrowing.ts, 201, 27))
528+
>type : Symbol(type, Decl(strictSubtypeAndNarrowing.ts, 201, 35))
529+
>a : Symbol(a, Decl(strictSubtypeAndNarrowing.ts, 201, 46))
530+
531+
assert(doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isA));
532+
>assert : Symbol(assert, Decl(strictSubtypeAndNarrowing.ts, 197, 47))
533+
>doesValueAtDeepPathSatisfy : Symbol(doesValueAtDeepPathSatisfy, Decl(strictSubtypeAndNarrowing.ts, 180, 12))
534+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 201, 15))
535+
>isA : Symbol(isA, Decl(strictSubtypeAndNarrowing.ts, 194, 79))
536+
537+
return foo;
538+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 201, 15))
539+
}
540+
541+
function test2(foo: Foo): {value: {type: 'A'}; a?: number} {
542+
>test2 : Symbol(test2, Decl(strictSubtypeAndNarrowing.ts, 204, 1))
543+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 206, 15))
544+
>Foo : Symbol(Foo, Decl(strictSubtypeAndNarrowing.ts, 191, 53))
545+
>value : Symbol(value, Decl(strictSubtypeAndNarrowing.ts, 206, 27))
546+
>type : Symbol(type, Decl(strictSubtypeAndNarrowing.ts, 206, 35))
547+
>a : Symbol(a, Decl(strictSubtypeAndNarrowing.ts, 206, 46))
548+
549+
assert(!doesValueAtDeepPathSatisfy(foo, ['value', 'type'], isB));
550+
>assert : Symbol(assert, Decl(strictSubtypeAndNarrowing.ts, 197, 47))
551+
>doesValueAtDeepPathSatisfy : Symbol(doesValueAtDeepPathSatisfy, Decl(strictSubtypeAndNarrowing.ts, 180, 12))
552+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 206, 15))
553+
>isB : Symbol(isB, Decl(strictSubtypeAndNarrowing.ts, 196, 47))
554+
555+
return foo;
556+
>foo : Symbol(foo, Decl(strictSubtypeAndNarrowing.ts, 206, 15))
557+
}
558+

0 commit comments

Comments
 (0)