GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Convert to async function: handle type arguments to then/catch by andrewbranch · Pull Request #37463 · microsoft/TypeScript

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Mar 18, 2020

Copy link
Copy Markdown
Member

I think this addresses what should be a real edge case, as there’s not a great reason to supply a type argument to .then<T>(...) or .catch<T>(...), but if you do, it can change the inferred return type of the async function. Since I can’t think of a good realistic example, here’s a silly contrived one:

// logs some stuff about any argument and returns it
declare function logWhenDebugging<T>(x: T): T;

// SOURCE
// inferred return type: Promise<User>
function fetch() {
  const promiseOfAny = Promise.resolve(undefined as any);
  return promiseOfAny.then<User>(x => logWhenDebugging(x));
}

// BEFORE: we drop the type argument
// inferred return type: Promise<any>
async function fetch() {
  const promiseOfAny = Promise.resolve(undefined as any);
  const x = await promiseOfAny;
  return logWhenDebugging(x);
}

// AFTER: new variable declaration to preserve it
// inferred return type: Promise<User>
async function fetch() {
  const promiseOfAny = Promise.resolve(undefined as any);
  const x = await promiseOfAny;
  const result: User = logWhenDebugging(x);
  return result;
}

Fixes #28529

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andrewbranch
Handle type arguments to then/catch
f751357
@andrewbranch
Andrew Branch (andrewbranch) requested review from Sheetal Nandi (sheetalkamat) and Ben Lichtman (uniqueiniquity) March 18, 2020 22:22
@typescript-bot TypeScript Bot (typescript-bot) assigned Andrew Branch (andrewbranch) Mar 18, 2020
@typescript-bot TypeScript Bot (typescript-bot) added the Author: Team label Mar 18, 2020
andrewbranch
Andrew Branch (andrewbranch) commented Mar 18, 2020
View reviewed changes
Comment thread tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_catchTypeArgument1.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Andrew Branch (andrewbranch) requested a review from Ron Buckton (rbuckton) March 18, 2020 22:23
Andrew Branch (andrewbranch) added 2 commits March 19, 2020 09:46
@andrewbranch
Merge branch 'master' into bug/convertToAsync
71a3ae3
@andrewbranch
Keep single-line types on a single line
afa0058
uniqueiniquity
Ben Lichtman (uniqueiniquity) approved these changes Mar 19, 2020
View reviewed changes
@andrewbranch
Andrew Branch (andrewbranch) merged commit 37569d0 into microsoft:master Mar 24, 2020
@andrewbranch
Andrew Branch (andrewbranch) deleted the bug/convertToAsync branch March 24, 2020 16:56
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@sheetalkamat Sheetal Nandi (sheetalkamat) Awaiting requested review from sheetalkamat
@rbuckton Ron Buckton (rbuckton) Awaiting requested review from rbuckton
1 more reviewer
@uniqueiniquity Ben Lichtman (uniqueiniquity) uniqueiniquity approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@andrewbranch Andrew Branch (andrewbranch)

Labels

Author: Team

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Convert to async function refactoring loses generic parameter

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@andrewbranch @uniqueiniquity @typescript-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.