microsoft / microsoft/TypeScript
Type inference inconsistency with generic constraints
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
- type inference const string
- type inference generic constraints
🕗 Version & Regression Information
Tested on
- 3.9.7
- 4.2.3
- 4.3.0-beta
⏯ Playground Link
💻 Code
Following Generic Constraints
type Concatable<T> = {
concat: (...xss: T[]) => T
};
export const concat = <T extends Concatable<T>>
(xs: T, ...xss: T[] ): T => xs.concat( ...xss );
export const test_1 = concat([1, 2], [3, 4]); // OK
export const test_2 = concat("foo" as string, "bar"); // OK
export const test_3 = concat<string>("foo", "bar"); // OK
// Expected: 'test_4: string'
// Got: 'test_4: Concatable<"foo" | "bar">'
export const test_4 = concat("foo", "bar");
// Error: ^^^^^
// Argument of type 'string' is not assignable to
// parameter of type 'Concatable<"foo" | "bar">'
🙁 Actual behavior
test_4: Concatable<"foo" | "bar">
TS leaks the constraint for type T into type T
TS provides no good way to make Rust-like trait
🙂 Expected behavior
T, test_4, "foo" and "bar" all infer type string
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’esempio di vincolo generico nel TypeScript Playground collegato nelle versioni elencate e controlla il tipo inferito di test_4. La correzione è completa quando concat("foo", "bar") inferisce string senza errori, in linea con il comportamento previsto, preservando gli altri esempi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100