Skip to content

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

Description

@sapphire-sh

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:

Activity

  1. sapphire-sh commented on Nov 21, 2019

    @sapphire-sh
    Author

    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
  2. MartinJohns commented on Nov 21, 2019

    @MartinJohns
    Contributor

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

  3. IllusionMH commented on Nov 21, 2019

    @IllusionMH
    Contributor

    #33707 might fix this one

  4. typescript-bot commented on Dec 21, 2019

    @typescript-bot
    Contributor

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

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions