Add extra test cases for the Awaited type (#53090)

This commit is contained in:
Mateusz Burzyński
2023-03-07 08:14:57 +01:00
committed by GitHub
parent df1ddb7964
commit 6dbec02a88
5 changed files with 231 additions and 1 deletions

View File

@@ -1,8 +1,9 @@
tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/compiler/awaitedType.ts(26,12): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/compiler/awaitedType.ts(236,28): error TS2493: Tuple type '[number]' of length '1' has no element at index '1'.
==== tests/cases/compiler/awaitedType.ts (2 errors) ====
==== tests/cases/compiler/awaitedType.ts (3 errors) ====
type T1 = Awaited<number>;
type T2 = Awaited<Promise<number>>;
type T3 = Awaited<number | Promise<number>>;
@@ -234,4 +235,30 @@ tests/cases/compiler/awaitedType.ts(26,12): error TS2589: Type instantiation is
const itsTwo1 = await findManyWrapper({ select: "foo" });
const itsTwo2 = await findManyWrapper({ include: "bar" });
}
// repro from #41831
{
const promises = [Promise.resolve(0)] as const
Promise.all(promises).then((results) => {
const first = results[0]
const second = results[1] // error
~
!!! error TS2493: Tuple type '[number]' of length '1' has no element at index '1'.
})
}
// repro from #40330
async function test40330() {
const promiseNumber = Promise.resolve(1);
const promiseVoid = async () => {}
const res = await Promise.all([
promiseNumber,
...[promiseVoid()]
])
}

View File

@@ -226,6 +226,30 @@ async function mainFindMany() {
const itsTwo1 = await findManyWrapper({ select: "foo" });
const itsTwo2 = await findManyWrapper({ include: "bar" });
}
// repro from #41831
{
const promises = [Promise.resolve(0)] as const
Promise.all(promises).then((results) => {
const first = results[0]
const second = results[1] // error
})
}
// repro from #40330
async function test40330() {
const promiseNumber = Promise.resolve(1);
const promiseVoid = async () => {}
const res = await Promise.all([
promiseNumber,
...[promiseVoid()]
])
}
//// [awaitedType.js]
@@ -355,3 +379,20 @@ async function mainFindMany() {
const itsTwo1 = await findManyWrapper({ select: "foo" });
const itsTwo2 = await findManyWrapper({ include: "bar" });
}
// repro from #41831
{
const promises = [Promise.resolve(0)];
Promise.all(promises).then((results) => {
const first = results[0];
const second = results[1]; // error
});
}
// repro from #40330
async function test40330() {
const promiseNumber = Promise.resolve(1);
const promiseVoid = async () => { };
const res = await Promise.all([
promiseNumber,
...[promiseVoid()]
]);
}

View File

@@ -596,3 +596,63 @@ async function mainFindMany() {
>include : Symbol(include, Decl(awaitedType.ts, 225, 41))
}
// repro from #41831
{
const promises = [Promise.resolve(0)] as const
>promises : Symbol(promises, Decl(awaitedType.ts, 231, 7))
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>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, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>const : Symbol(const)
Promise.all(promises).then((results) => {
>Promise.all(promises).then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>Promise.all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>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, --, --))
>all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>promises : Symbol(promises, Decl(awaitedType.ts, 231, 7))
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>results : Symbol(results, Decl(awaitedType.ts, 233, 30))
const first = results[0]
>first : Symbol(first, Decl(awaitedType.ts, 234, 9))
>results : Symbol(results, Decl(awaitedType.ts, 233, 30))
>0 : Symbol(0)
const second = results[1] // error
>second : Symbol(second, Decl(awaitedType.ts, 235, 9))
>results : Symbol(results, Decl(awaitedType.ts, 233, 30))
})
}
// repro from #40330
async function test40330() {
>test40330 : Symbol(test40330, Decl(awaitedType.ts, 237, 1))
const promiseNumber = Promise.resolve(1);
>promiseNumber : Symbol(promiseNumber, Decl(awaitedType.ts, 243, 9))
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>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, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
const promiseVoid = async () => {}
>promiseVoid : Symbol(promiseVoid, Decl(awaitedType.ts, 244, 9))
const res = await Promise.all([
>res : Symbol(res, Decl(awaitedType.ts, 246, 9))
>Promise.all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>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, --, --))
>all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
promiseNumber,
>promiseNumber : Symbol(promiseNumber, Decl(awaitedType.ts, 243, 9))
...[promiseVoid()]
>promiseVoid : Symbol(promiseVoid, Decl(awaitedType.ts, 244, 9))
])
}

View File

@@ -542,3 +542,81 @@ async function mainFindMany() {
>"bar" : "bar"
}
// repro from #41831
{
const promises = [Promise.resolve(0)] as const
>promises : readonly [Promise<number>]
>[Promise.resolve(0)] as const : readonly [Promise<number>]
>[Promise.resolve(0)] : readonly [Promise<number>]
>Promise.resolve(0) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>0 : 0
Promise.all(promises).then((results) => {
>Promise.all(promises).then((results) => { const first = results[0] const second = results[1] // error }) : Promise<void>
>Promise.all(promises).then : <TResult1 = [number], TResult2 = never>(onfulfilled?: (value: [number]) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>Promise.all(promises) : Promise<[number]>
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>Promise : PromiseConstructor
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>promises : readonly [Promise<number>]
>then : <TResult1 = [number], TResult2 = never>(onfulfilled?: (value: [number]) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
>(results) => { const first = results[0] const second = results[1] // error } : (results: [number]) => void
>results : [number]
const first = results[0]
>first : number
>results[0] : number
>results : [number]
>0 : 0
const second = results[1] // error
>second : undefined
>results[1] : undefined
>results : [number]
>1 : 1
})
}
// repro from #40330
async function test40330() {
>test40330 : () => Promise<void>
const promiseNumber = Promise.resolve(1);
>promiseNumber : Promise<number>
>Promise.resolve(1) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>1 : 1
const promiseVoid = async () => {}
>promiseVoid : () => Promise<void>
>async () => {} : () => Promise<void>
const res = await Promise.all([
>res : [number, ...void[]]
>await Promise.all([ promiseNumber, ...[promiseVoid()] ]) : [number, ...void[]]
>Promise.all([ promiseNumber, ...[promiseVoid()] ]) : Promise<[number, ...void[]]>
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>Promise : PromiseConstructor
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>[ promiseNumber, ...[promiseVoid()] ] : [Promise<number>, ...Promise<void>[]]
promiseNumber,
>promiseNumber : Promise<number>
...[promiseVoid()]
>...[promiseVoid()] : Promise<void>
>[promiseVoid()] : Promise<void>[]
>promiseVoid() : Promise<void>
>promiseVoid : () => Promise<void>
])
}

View File

@@ -228,3 +228,27 @@ async function mainFindMany() {
const itsTwo1 = await findManyWrapper({ select: "foo" });
const itsTwo2 = await findManyWrapper({ include: "bar" });
}
// repro from #41831
{
const promises = [Promise.resolve(0)] as const
Promise.all(promises).then((results) => {
const first = results[0]
const second = results[1] // error
})
}
// repro from #40330
async function test40330() {
const promiseNumber = Promise.resolve(1);
const promiseVoid = async () => {}
const res = await Promise.all([
promiseNumber,
...[promiseVoid()]
])
}