- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
Adds 'Awaited' type alias and updates to Promise.all/race/allSettled/any #45350
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| tests/cases/compiler/awaitedType.ts(18,12): error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
| tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|  | ||
|  | ||
| ==== tests/cases/compiler/awaitedType.ts (2 errors) ==== | ||
| type T1 = Awaited<number>; | ||
| type T2 = Awaited<Promise<number>>; | ||
| type T3 = Awaited<number | Promise<number>>; | ||
| type T4 = Awaited<number | Promise<string>>; | ||
| type T5 = Awaited<{ then: number }>; | ||
| type T6 = Awaited<{ then(): void }>; // never (non-promise "thenable") | ||
| type T7 = Awaited<{ then(x: number): void }>; // never (non-promise "thenable") | ||
| type T8 = Awaited<{ then(x: () => void): void }>; // unknown | ||
| type T9 = Awaited<any>; | ||
| type T10 = Awaited<never>; | ||
| type T11 = Awaited<unknown>; | ||
| type T12 = Awaited<Promise<Promise<number>>>; | ||
| type T13 = _Expect<Awaited<Promise<Promise<number>> | string | null>, /*expected*/ string | number | null>; // otherwise just prints T13 in types tests, which isn't very helpful | ||
| type T14 = _Expect<Awaited<Promise<Promise<number>> | string | undefined>, /*expected*/ string | number | undefined>; // otherwise just prints T14 in types tests, which isn't very helpful | ||
| type T15 = _Expect<Awaited<Promise<Promise<number>> | string | null | undefined>, /*expected*/ string | number | null | undefined>; // otherwise just prints T15 in types tests, which isn't very helpful | ||
|  | ||
| interface BadPromise { then(cb: (value: BadPromise) => void): void; } | ||
| type T16 = Awaited<BadPromise>; // error | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|  | ||
| interface BadPromise1 { then(cb: (value: BadPromise2) => void): void; } | ||
| interface BadPromise2 { then(cb: (value: BadPromise1) => void): void; } | ||
| type T17 = Awaited<BadPromise1>; // error | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|  | ||
| // https://github.com/microsoft/TypeScript/issues/33562 | ||
| type MaybePromise<T> = T | Promise<T> | PromiseLike<T> | ||
| declare function MaybePromise<T>(value: T): MaybePromise<T>; | ||
|  | ||
| async function main() { | ||
| let aaa: number; | ||
| let bbb: string; | ||
| [ | ||
| aaa, | ||
| bbb, | ||
| ] = await Promise.all([ | ||
| MaybePromise(1), | ||
| MaybePromise('2'), | ||
| MaybePromise(true), | ||
| ]) | ||
| } | ||
|  | ||
| // non-generic | ||
| async function f1(x: string) { | ||
| // y: string | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f2(x: unknown) { | ||
| // y: unknown | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f3(x: object) { | ||
| // y: object | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f4(x: Promise<string>) { | ||
| // y: string | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f5(x: Promise<unknown>) { | ||
| // y: unknown | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f6(x: Promise<object>) { | ||
| // y: object | ||
| const y = await x; | ||
| } | ||
|  | ||
| // generic | ||
|  | ||
| async function f7<T>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f8<T extends any>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f9<T extends unknown>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f10<T extends {}>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f11<T extends { then(onfulfilled: (value: unknown) => void): void }>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f12<T extends string | object>(x: T) { | ||
| // NOTE: T does not belong solely to the domain of primitive types and either does | ||
| // not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`, | ||
| // or it has a non-primitive base constraint with a callable `then`. | ||
|  | ||
| // y: Awaited<T> | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f13<T extends string>(x: T) { | ||
| // NOTE: T belongs to the domain of primitive types | ||
|  | ||
| // y: T | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f14<T extends { x: number }>(x: T) { | ||
| // NOTE: T has a non-primitive base constraint without a callable `then`. | ||
|  | ||
| // y: T | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f15<T extends { then: number }>(x: T) { | ||
| // NOTE: T has a non-primitive base constraint without a callable `then`. | ||
|  | ||
| // y: T | ||
| const y = await x; | ||
| } | ||
|  | ||
| async function f16<T extends number & { then(): void }>(x: T) { | ||
| // NOTE: T belongs to the domain of primitive types (regardless of `then`) | ||
|  | ||
| // y: T | ||
| const y = await x; | ||
| } | ||
|  | ||
|  | ||
| // helps with tests where '.types' just prints out the type alias name | ||
| type _Expect<TActual extends TExpected, TExpected> = TActual; | ||
|  | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that these comments are swapped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently so. I will push up a small fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by #45918