-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.7.2 Promise.all infers base type of interfaces rather than actual interface #34937
Comments
type Awaited<T> =
T extends undefined ?
T :
T extends PromiseLike<infer U> ?
U :
T
;
declare function all<T extends readonly any[]>(
values: T
): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;
//for empty tuple
declare function betterAll (arr : []) : Promise<[]>;
//for non-empty tuple
declare function betterAll<ArrT extends readonly [any, ...any[]]>(
arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;
//for non-tuple array
declare function betterAll<ArrT extends readonly any[]>(
arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;
//https://github.com/microsoft/TypeScript/issues/34937
interface QuoteFilter {
_id: string;
}
interface QuoteFilterAdvanced extends QuoteFilter {
name: string;
}
function getQuoteFilters(): Promise<QuoteFilter[]> {
return new Promise<QuoteFilter[]>(
(resolve, reject) => {
resolve([
{ _id: 'Test' }
]);
});
}
function getQuoteFiltersAdvanced(): Promise<QuoteFilterAdvanced[]> {
return new Promise<QuoteFilterAdvanced[]>(
(resolve, reject) => {
resolve([
{ _id: 'Test', name: 'Test' }
]);
});
}
async () => {
//const filters: QuoteFilter[]
//const advanced: QuoteFilter[]
const [filters, advanced] = await Promise.all([
getQuoteFilters(),
getQuoteFiltersAdvanced()
]);
}
async () => {
//const filters: QuoteFilter[]
//const advanced: QuoteFilterAdvanced[]
const [filters, advanced] = await all([
getQuoteFilters(),
getQuoteFiltersAdvanced()
]);
}
async () => {
//const filters: QuoteFilter[]
//const advanced: QuoteFilterAdvanced[]
const [filters, advanced] = await betterAll([
getQuoteFilters(),
getQuoteFiltersAdvanced()
]);
} See #33707 |
Same here. This could look a bug |
The same issue occurs even if you don't explicitly inherit types: playground, which looks very odd. |
This looks like a bug. I found |
@XGHeaven I believe the incorrect results you are getting are indeed from the overload in |
@jablko |
seeing same issue here... undefined is added to everything I call in a Promise.all... |
@XGHeaven I see what you mean, however I think you've highlighted a separate issue with the playground: That example doesn't work in the playground regardless of version (3.7.2 or 3.6.3). https://www.typescriptlang.org/play/index.html?ts=3.6.3#code/IYZwngdgxgBAZgV2gFwJYHsIwOYFNkCCANkQJLK4C2IAFAJQwDeAUDGzFJiMjANpyoATtwA0MELk4QAJgF0AXHwgJKAI1yCx3QagjZZMALwxgAd2CoeABUHpKqCQDpgJGrwCMYgORfZdANzMAL5AA I think it's a symptom of microsoft/TypeScript-Website#82 (the playground doesn't use the lib d.ts files that get shipped with a build). In my tests (locally) it doesn't use |
I found something! Is it possible that the added In my case, all types from all Promises are mashed together in TypeScript 3.7.2, not in 3.6.3. As far as I can tell by looking at In 3.7.2:
In 3.6.3:
The only difference there is the added This was changed here: 29becf0#diff-ca94294a2cb9f29f16a30f5d32e56c2e |
A workaround for this is to use |
Similar issue. This works in async function foo(): Promise<number> {
const [first, second] = await Promise.all([generateNumber(), generateStringOrNull()]);
return first; // ERROR: Type 'number | null' is not assignable to type 'number'.
}
async function generateNumber(): Promise<number> {
return 10;
}
async function generateStringOrNull(): Promise<string| null> {
return '10';
}
I can confirm that workaround works for me. |
We've also hit with this when using 3.7.3. I can too confirm that the |
TypeScript 3.7.2
Playground link
Compiler Options:
Input:
Output:
Expected behavior:
I am expecting the advanced variable to be an array of QuoteFilterAdvanced rather than QuoteFilter.
This was correctly inferred in Typescript 3.6.3
The text was updated successfully, but these errors were encountered: