microsoft / microsoft/TypeScript

Type widening for T or Promise<T>

Open
#45,338 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

Type widening, promise, generic type widening.

🕗 Version & Regression Information

This is the behavior in every version I tried.

⏯ Playground Link

Playground link

💻 Code
type Cb3 = <Res extends string | Promise<string>>(cb: () => Res) => Res

const c: Cb3 = {} as any;

// this don't widen the type, type of t31 is "1"
const t31 = c(() => {
  return '1';
});

// but here is the problem, type of t32 is Promise<string>
const t32 = c(async () => {
  return '1';
});
🙁 Actual behavior

Type of t32 is Promise<string>

🙂 Expected behavior

Type of t32 is Promise<"1">, by analogy with type of t31

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and compare generic callback inference in the synchronous and async examples. Trace where the Promise case loses the literal type; done means the async call is inferred as Promise<"1"> while the synchronous result remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.