Skip to content

Commit a34d5ae

Browse files
authored
Add extra test cases for Awaited<T> inference (#53068)
1 parent 4b6fb95 commit a34d5ae

File tree

5 files changed

+378
-3
lines changed

5 files changed

+378
-3
lines changed

tests/baselines/reference/awaitedType.errors.txt

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,47 @@ tests/cases/compiler/awaitedType.ts(26,12): error TS2589: Type instantiation is
191191
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
192192
const structure = await structurePromise;
193193
structure[key] = 1;
194-
}
194+
}
195+
196+
// repro from #46543
197+
198+
type SelectAndInclude = {
199+
select: any;
200+
include: any;
201+
};
202+
type HasSelect = {
203+
select: any;
204+
};
205+
type HasInclude = {
206+
include: any;
207+
};
208+
209+
type CheckSelect<T, S, U> = T extends SelectAndInclude
210+
? "Please either choose `select` or `include`"
211+
: T extends HasSelect
212+
? U
213+
: T extends HasInclude
214+
? U
215+
: S;
216+
217+
declare function findMany<T extends { select?: string; include?: string }>(
218+
args: T
219+
): CheckSelect<T, Promise<1>, Promise<2>>;
220+
221+
async function findManyWrapper<
222+
T extends { select?: string; include?: string }
223+
>(args: T) {
224+
const result = await findMany(args);
225+
return result;
226+
}
227+
228+
async function mainFindMany() {
229+
const shouldBeErrorText = await findManyWrapper({
230+
select: "foo",
231+
include: "bar",
232+
});
233+
const itsOne = await findManyWrapper({});
234+
const itsTwo1 = await findManyWrapper({ select: "foo" });
235+
const itsTwo2 = await findManyWrapper({ include: "bar" });
236+
}
237+

tests/baselines/reference/awaitedType.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,50 @@ type GenericStructure<
183183
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
184184
const structure = await structurePromise;
185185
structure[key] = 1;
186-
}
186+
}
187+
188+
// repro from #46543
189+
190+
type SelectAndInclude = {
191+
select: any;
192+
include: any;
193+
};
194+
type HasSelect = {
195+
select: any;
196+
};
197+
type HasInclude = {
198+
include: any;
199+
};
200+
201+
type CheckSelect<T, S, U> = T extends SelectAndInclude
202+
? "Please either choose `select` or `include`"
203+
: T extends HasSelect
204+
? U
205+
: T extends HasInclude
206+
? U
207+
: S;
208+
209+
declare function findMany<T extends { select?: string; include?: string }>(
210+
args: T
211+
): CheckSelect<T, Promise<1>, Promise<2>>;
212+
213+
async function findManyWrapper<
214+
T extends { select?: string; include?: string }
215+
>(args: T) {
216+
const result = await findMany(args);
217+
return result;
218+
}
219+
220+
async function mainFindMany() {
221+
const shouldBeErrorText = await findManyWrapper({
222+
select: "foo",
223+
include: "bar",
224+
});
225+
const itsOne = await findManyWrapper({});
226+
const itsTwo1 = await findManyWrapper({ select: "foo" });
227+
const itsTwo2 = await findManyWrapper({ include: "bar" });
228+
}
229+
187230

188231
//// [awaitedType.js]
189232
async function main() {
@@ -299,3 +342,16 @@ async function brokenExample(structurePromise, key) {
299342
const structure = await structurePromise;
300343
structure[key] = 1;
301344
}
345+
async function findManyWrapper(args) {
346+
const result = await findMany(args);
347+
return result;
348+
}
349+
async function mainFindMany() {
350+
const shouldBeErrorText = await findManyWrapper({
351+
select: "foo",
352+
include: "bar",
353+
});
354+
const itsOne = await findManyWrapper({});
355+
const itsTwo1 = await findManyWrapper({ select: "foo" });
356+
const itsTwo2 = await findManyWrapper({ include: "bar" });
357+
}

tests/baselines/reference/awaitedType.symbols

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,123 @@ async function brokenExample<AcceptableKeyType extends string = string>(structur
476476
>structure : Symbol(structure, Decl(awaitedType.ts, 182, 7))
477477
>key : Symbol(key, Decl(awaitedType.ts, 181, 135))
478478
}
479+
480+
// repro from #46543
481+
482+
type SelectAndInclude = {
483+
>SelectAndInclude : Symbol(SelectAndInclude, Decl(awaitedType.ts, 184, 1))
484+
485+
select: any;
486+
>select : Symbol(select, Decl(awaitedType.ts, 188, 25))
487+
488+
include: any;
489+
>include : Symbol(include, Decl(awaitedType.ts, 189, 14))
490+
491+
};
492+
type HasSelect = {
493+
>HasSelect : Symbol(HasSelect, Decl(awaitedType.ts, 191, 2))
494+
495+
select: any;
496+
>select : Symbol(select, Decl(awaitedType.ts, 192, 18))
497+
498+
};
499+
type HasInclude = {
500+
>HasInclude : Symbol(HasInclude, Decl(awaitedType.ts, 194, 2))
501+
502+
include: any;
503+
>include : Symbol(include, Decl(awaitedType.ts, 195, 19))
504+
505+
};
506+
507+
type CheckSelect<T, S, U> = T extends SelectAndInclude
508+
>CheckSelect : Symbol(CheckSelect, Decl(awaitedType.ts, 197, 2))
509+
>T : Symbol(T, Decl(awaitedType.ts, 199, 17))
510+
>S : Symbol(S, Decl(awaitedType.ts, 199, 19))
511+
>U : Symbol(U, Decl(awaitedType.ts, 199, 22))
512+
>T : Symbol(T, Decl(awaitedType.ts, 199, 17))
513+
>SelectAndInclude : Symbol(SelectAndInclude, Decl(awaitedType.ts, 184, 1))
514+
515+
? "Please either choose `select` or `include`"
516+
: T extends HasSelect
517+
>T : Symbol(T, Decl(awaitedType.ts, 199, 17))
518+
>HasSelect : Symbol(HasSelect, Decl(awaitedType.ts, 191, 2))
519+
520+
? U
521+
>U : Symbol(U, Decl(awaitedType.ts, 199, 22))
522+
523+
: T extends HasInclude
524+
>T : Symbol(T, Decl(awaitedType.ts, 199, 17))
525+
>HasInclude : Symbol(HasInclude, Decl(awaitedType.ts, 194, 2))
526+
527+
? U
528+
>U : Symbol(U, Decl(awaitedType.ts, 199, 22))
529+
530+
: S;
531+
>S : Symbol(S, Decl(awaitedType.ts, 199, 19))
532+
533+
declare function findMany<T extends { select?: string; include?: string }>(
534+
>findMany : Symbol(findMany, Decl(awaitedType.ts, 205, 6))
535+
>T : Symbol(T, Decl(awaitedType.ts, 207, 26))
536+
>select : Symbol(select, Decl(awaitedType.ts, 207, 37))
537+
>include : Symbol(include, Decl(awaitedType.ts, 207, 54))
538+
539+
args: T
540+
>args : Symbol(args, Decl(awaitedType.ts, 207, 75))
541+
>T : Symbol(T, Decl(awaitedType.ts, 207, 26))
542+
543+
): CheckSelect<T, Promise<1>, Promise<2>>;
544+
>CheckSelect : Symbol(CheckSelect, Decl(awaitedType.ts, 197, 2))
545+
>T : Symbol(T, Decl(awaitedType.ts, 207, 26))
546+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
547+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
548+
549+
async function findManyWrapper<
550+
>findManyWrapper : Symbol(findManyWrapper, Decl(awaitedType.ts, 209, 42))
551+
552+
T extends { select?: string; include?: string }
553+
>T : Symbol(T, Decl(awaitedType.ts, 211, 31))
554+
>select : Symbol(select, Decl(awaitedType.ts, 212, 13))
555+
>include : Symbol(include, Decl(awaitedType.ts, 212, 30))
556+
557+
>(args: T) {
558+
>args : Symbol(args, Decl(awaitedType.ts, 213, 2))
559+
>T : Symbol(T, Decl(awaitedType.ts, 211, 31))
560+
561+
const result = await findMany(args);
562+
>result : Symbol(result, Decl(awaitedType.ts, 214, 7))
563+
>findMany : Symbol(findMany, Decl(awaitedType.ts, 205, 6))
564+
>args : Symbol(args, Decl(awaitedType.ts, 213, 2))
565+
566+
return result;
567+
>result : Symbol(result, Decl(awaitedType.ts, 214, 7))
568+
}
569+
570+
async function mainFindMany() {
571+
>mainFindMany : Symbol(mainFindMany, Decl(awaitedType.ts, 216, 1))
572+
573+
const shouldBeErrorText = await findManyWrapper({
574+
>shouldBeErrorText : Symbol(shouldBeErrorText, Decl(awaitedType.ts, 219, 7))
575+
>findManyWrapper : Symbol(findManyWrapper, Decl(awaitedType.ts, 209, 42))
576+
577+
select: "foo",
578+
>select : Symbol(select, Decl(awaitedType.ts, 219, 51))
579+
580+
include: "bar",
581+
>include : Symbol(include, Decl(awaitedType.ts, 220, 18))
582+
583+
});
584+
const itsOne = await findManyWrapper({});
585+
>itsOne : Symbol(itsOne, Decl(awaitedType.ts, 223, 7))
586+
>findManyWrapper : Symbol(findManyWrapper, Decl(awaitedType.ts, 209, 42))
587+
588+
const itsTwo1 = await findManyWrapper({ select: "foo" });
589+
>itsTwo1 : Symbol(itsTwo1, Decl(awaitedType.ts, 224, 7))
590+
>findManyWrapper : Symbol(findManyWrapper, Decl(awaitedType.ts, 209, 42))
591+
>select : Symbol(select, Decl(awaitedType.ts, 224, 41))
592+
593+
const itsTwo2 = await findManyWrapper({ include: "bar" });
594+
>itsTwo2 : Symbol(itsTwo2, Decl(awaitedType.ts, 225, 7))
595+
>findManyWrapper : Symbol(findManyWrapper, Decl(awaitedType.ts, 209, 42))
596+
>include : Symbol(include, Decl(awaitedType.ts, 225, 41))
597+
}
598+

tests/baselines/reference/awaitedType.types

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,117 @@ async function brokenExample<AcceptableKeyType extends string = string>(structur
428428
>key : AcceptableKeyType
429429
>1 : 1
430430
}
431+
432+
// repro from #46543
433+
434+
type SelectAndInclude = {
435+
>SelectAndInclude : { select: any; include: any; }
436+
437+
select: any;
438+
>select : any
439+
440+
include: any;
441+
>include : any
442+
443+
};
444+
type HasSelect = {
445+
>HasSelect : { select: any; }
446+
447+
select: any;
448+
>select : any
449+
450+
};
451+
type HasInclude = {
452+
>HasInclude : { include: any; }
453+
454+
include: any;
455+
>include : any
456+
457+
};
458+
459+
type CheckSelect<T, S, U> = T extends SelectAndInclude
460+
>CheckSelect : CheckSelect<T, S, U>
461+
462+
? "Please either choose `select` or `include`"
463+
: T extends HasSelect
464+
? U
465+
: T extends HasInclude
466+
? U
467+
: S;
468+
469+
declare function findMany<T extends { select?: string; include?: string }>(
470+
>findMany : <T extends { select?: string; include?: string; }>(args: T) => CheckSelect<T, Promise<1>, Promise<2>>
471+
>select : string
472+
>include : string
473+
474+
args: T
475+
>args : T
476+
477+
): CheckSelect<T, Promise<1>, Promise<2>>;
478+
479+
async function findManyWrapper<
480+
>findManyWrapper : <T extends { select?: string; include?: string; }>(args: T) => Promise<CheckSelect<T, Promise<1>, Promise<2>>>
481+
482+
T extends { select?: string; include?: string }
483+
>select : string
484+
>include : string
485+
486+
>(args: T) {
487+
>args : T
488+
489+
const result = await findMany(args);
490+
>result : Awaited<CheckSelect<T, Promise<1>, Promise<2>>>
491+
>await findMany(args) : Awaited<CheckSelect<T, Promise<1>, Promise<2>>>
492+
>findMany(args) : CheckSelect<T, Promise<1>, Promise<2>>
493+
>findMany : <T extends { select?: string; include?: string; }>(args: T) => CheckSelect<T, Promise<1>, Promise<2>>
494+
>args : T
495+
496+
return result;
497+
>result : Awaited<CheckSelect<T, Promise<1>, Promise<2>>>
498+
}
499+
500+
async function mainFindMany() {
501+
>mainFindMany : () => Promise<void>
502+
503+
const shouldBeErrorText = await findManyWrapper({
504+
>shouldBeErrorText : "Please either choose `select` or `include`"
505+
>await findManyWrapper({ select: "foo", include: "bar", }) : "Please either choose `select` or `include`"
506+
>findManyWrapper({ select: "foo", include: "bar", }) : Promise<"Please either choose `select` or `include`">
507+
>findManyWrapper : <T extends { select?: string; include?: string; }>(args: T) => Promise<CheckSelect<T, Promise<1>, Promise<2>>>
508+
>{ select: "foo", include: "bar", } : { select: string; include: string; }
509+
510+
select: "foo",
511+
>select : string
512+
>"foo" : "foo"
513+
514+
include: "bar",
515+
>include : string
516+
>"bar" : "bar"
517+
518+
});
519+
const itsOne = await findManyWrapper({});
520+
>itsOne : 1
521+
>await findManyWrapper({}) : 1
522+
>findManyWrapper({}) : Promise<Promise<1>>
523+
>findManyWrapper : <T extends { select?: string; include?: string; }>(args: T) => Promise<CheckSelect<T, Promise<1>, Promise<2>>>
524+
>{} : {}
525+
526+
const itsTwo1 = await findManyWrapper({ select: "foo" });
527+
>itsTwo1 : 2
528+
>await findManyWrapper({ select: "foo" }) : 2
529+
>findManyWrapper({ select: "foo" }) : Promise<Promise<2>>
530+
>findManyWrapper : <T extends { select?: string; include?: string; }>(args: T) => Promise<CheckSelect<T, Promise<1>, Promise<2>>>
531+
>{ select: "foo" } : { select: string; }
532+
>select : string
533+
>"foo" : "foo"
534+
535+
const itsTwo2 = await findManyWrapper({ include: "bar" });
536+
>itsTwo2 : 2
537+
>await findManyWrapper({ include: "bar" }) : 2
538+
>findManyWrapper({ include: "bar" }) : Promise<Promise<2>>
539+
>findManyWrapper : <T extends { select?: string; include?: string; }>(args: T) => Promise<CheckSelect<T, Promise<1>, Promise<2>>>
540+
>{ include: "bar" } : { include: string; }
541+
>include : string
542+
>"bar" : "bar"
543+
}
544+

0 commit comments

Comments
 (0)