TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Intellisense doesn't narrow properly when discriminator is a template literal

Open gordonmleigh opened this issue 10 months ago • 0 comments

🔎 Search Terms

"intellisense template literal", "intellisense property union"

🕗 Version & Regression Information

  • This is the behavior in every version I tried (v4.9.5, v5.8.2)

⏯ Playground Link

No response

💻 Code

I didn't include a playground link since it's an intellisense issue.

type A = {
  id: `A:${string}`;
  a: number;
};

type B = {
  id: `B:${string}`;
  b: number;
};

function foo<T extends A | B>(item: T): void {}

foo({ id: "B:" /* auto-complete here */ });

🙁 Actual behavior

When you trigger intellisense, it suggests all the property names of the union, even if it could already narrow it based on what you've typed so far:

Image

If id is a literal rather than a template literal type, the suggestions are properly narrowed.

🙂 Expected behavior

I think the property suggestions should be limited to the narrowed type, similar to if the discriminant is an ordinary string literal.

Additional information about the issue

No response

gordonmleigh avatar Mar 04 '25 20:03 gordonmleigh