microsoft / microsoft/TypeScript

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

Aperta
#57,872 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il confronto nel TypeScript Playground collegato e leggi l’issue correlata 45201 per il contesto. Traccia il modo in cui il verificatore dei tipi confronta i tipi literal di template; il lavoro è completato quando segnala la diagnostica no-overlap mancante per x e y senza introdurre regressioni nel comportamento esistente di a e b.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.