Skip to content
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

Insufficient type narrowing of return type with Promise.all #35258

Closed
sapphire-sh opened this issue Nov 21, 2019 · 4 comments · Fixed by #45350
Closed

Insufficient type narrowing of return type with Promise.all #35258

sapphire-sh opened this issue Nov 21, 2019 · 4 comments · Fixed by #45350
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@sapphire-sh
Copy link

TypeScript Version: 3.7.2

Search Terms:
generics readonly Promise.all

Code

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "strictNullChecks": true,
    "esModuleInterop": true,
  }
}
declare function x1(): Promise<number>;
declare function x2(): Promise<number | null>;

(async () => {
	const [a, b] = await Promise.all([x1(), x2()]);
	if (a < 1) { }
})();

Expected behavior:
type of a should be number

Actual behavior:
type of a is number | null

Playground Link:
https://www.typescriptlang.org/play/index.html?noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&esModuleInterop=false&ssl=1&ssc=1&pln=8&pc=1#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwA8BGACgEoAueABRhwFssBnEAHlWXoCMQYA+ANwAoUJFgIU6bHkIAmclVoNmbDt17wAPvA4QIgoUJJQmAT3Txy8ALx94AbyEBIMHiYZ4AbSgAaeFwBdG3goAHcoLA8lRhYAOig9Ek9icj8CeTIAsmEnLERLKHhWeCIyB3gAXyEKsnJhIA

Related Issues:

@sapphire-sh
Copy link
Author

sapphire-sh commented Nov 21, 2019

another example:

declare class C<X> { }

declare function fn<T1, T2>(values: readonly [C<T1>, C<T2>]): [T1, T2];

declare function x1(): C<number>;
declare function x2(): C<number | null>;

(async () => {
	const [a, b] = fn([x1(), x2()]);
	if (a < 1) { }
})();

this code works if there is

  • no readonly modifer
  • or compiler option of strictNullChecks: false
  • or no generic class

@MartinJohns
Copy link
Contributor

Duplicate of #35136, #34925, #33562. Found by searching for Promise.all.

@IllusionMH
Copy link
Contributor

#33707 might fix this one

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants