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

Convert to async function: handle type arguments to then/catch #37463

Merged
merged 3 commits into from
Mar 24, 2020

Conversation

andrewbranch
Copy link
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

@andrewbranch andrewbranch requested a review from rbuckton March 18, 2020 22:23
@andrewbranch andrewbranch merged commit 37569d0 into microsoft:master Mar 24, 2020
@andrewbranch andrewbranch deleted the bug/convertToAsync branch March 24, 2020 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert to async function refactoring loses generic parameter
3 participants