microsoft / microsoft/TypeScript

Improve comparable relation of template literal types to detect more impossible situations

Open
#57,872 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

🔎 Search Terms

string condition always return false
2367 template

🕗 Version & Regression Information

This is the behavior in every version after 4.4.4 (including nightly), and I reviewed the FAQ for entries about "template" or "string"

This issue claims to fix a related problem in 4.5, but 4.5.5 is the first issue that fails for me. Maybe no relation though.
https://github.com/microsoft/TypeScript/issues/45201

In 4.4.4 it it fails correctly though, so maybe that fix is the regression of this issue?! https://www.typescriptlang.org/play?ts=4.4.4#code/C4TwDgpgBAglC8UAGBDAXGgJAbwHYFcBbAIwgCcBfJAKFEigCEFliMcCTyrrbxoANZkgAebPEVKUadaAE0h7CVxrUAxgHtcAZ2BR0sIeizYAjNw3bdrRkNbGzKizqiiogxCLEO1m5yDRQ8h443tQAlgBmUAAUwgjwiCAAlFDY1FBQTuoANhAAdNnqAObRAOTAABZhWlBaFer42QAmUBUoAG7QEShhuU2lSdQUPJExKPGIxClpGVm5BcVl3b01GmRkEKrA2SADQ9RAA

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UAGBDAXGgJAbwHYFcBbAIwgCcBfJAKFEigCEFliMcCTyrrbxoANZkgAebPEVKUadaAE0h7CVxrUAxgHtcAZ2BR0sIeizYAjNw3bdrRkNbGzKizqiiogxCLEO1m5yDRQ8h443tQAlgBmUAAUwgjwiCAAlFDY1FBQTuoANhAAdNnqAObRAOTAABZhWlBaFer42QAmUBUoAG7QEShhuU2lSdQUPJExKPGIxClpGVm5BcVl3b01GmRkEKrA2SADQ9RAA

💻 Code
type A = `a::${number}`
type B = `b::${number}`

type X = `x::${number}`
type Y = `${number}`

const a: A = `a::${1}`
const b: B = `b::${1}`

const x: X = `x::${1}`
const y: Y = `${1}`

if (x === y) {
  console.log('this should have failed')
}

if (a === b) {
  console.log('fails correctly')
}

🙁 Actual behavior

Comparing x and y generates no complaints from TS for "x::1" and "1", but does for "a::1" and "b::1". There is no way for a number to evaluate to "x:: so there is no overlap between these template types.

🙂 Expected behavior

I would expect that Typescript shows that error consistently whenever types don't match.

There is no overlap between a string that must start with "x::" and one that must start with a number.

Additional information about the issue

No response

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 comparison in the linked TypeScript Playground and read the related issue 45201 for context. Trace how the type checker compares the template literal types; done means it reports the missing no-overlap diagnostic for x and y without regressing the existing a and b behavior.

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.