Skip to content

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
@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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert to async function refactoring loses generic parameter

3 participants