microsoft / microsoft/TypeScript

NoInfer doesn't work in string template types

Open
#59,826 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

NoInfer string template

🕗 Version & Regression Information

This issue exists in every Playground-accessible version that has NoInfer.

⏯ Playground Link

here

💻 Code
function f<A>(a: NoInfer<A>, b: A extends string ? `rel:${A}` : never) {}
f("5", "rel:7"); // error - correct, type of A is "7"

but

function g<A>(a: A, b: A extends string ? `rel:${NoInfer<A>}` : never) {}
g("5", "rel:7"); // no error - surprising - type of A is inferred to be ("5" | "7")
🙁 Actual behavior

no type error on g() above, because the A type is being inferred via the string template in spite of the NoInfer marker.

🙂 Expected behavior

Both of the example calls should be recognized as wrong?

Additional information about the issue

Also tried

function g<A>(a: A, b: A extends string ? NoInfer<`rel:${A}`> : never) {}
function g<A>(a: A, b: NoInfer<A extends string ? `rel:${A}` : never>) {}

no luck.

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 by reproducing the two generic-call examples in the linked TypeScript Playground and compare how NoInfer behaves in the parameter and string-template forms. Trace the type inference behavior for NoInfer inside template literal types; done means the g("5", "rel:7") call is rejected as expected, with coverage for the shown variants.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.