microsoft / microsoft/TypeScript
Generic parameter type checking stops too early
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
generic, early, permature, defer
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link
💻 Code
interface RefObject<T> {
current: T | null
}
declare function test(x: RefObject<number>): void;
declare function getRef(): RefObject<number | null>;
let v = getRef();
test(v);
🙁 Actual behavior
In the above code, RefObject is defined to always accept null in its current property. However, if one explicitly writes RefObject<number | null> and passes the result to a parameter accepting RefObject<number> TS reports a type error even though the types of the current property would have been the same. This happens with any type, not just null (e.g. if the definition of current was T | string.
🙂 Expected behavior
Since the current properties are equivalent after substituting the generic parameters, there should be no error. It seems like TS is stopping as soon as it sees RefObject<number | null> vs RefObject<number> even though substituting the generic parameter would result in the same type. This can be confirmed by creating a copy of the RefObject type like this:
interface RefObject<T> {
current: T | null
}
interface RefObject2<T> {
current: T | null
}
declare function test(x: RefObject<number>): void;
declare function getRef(): RefObject2<number | null>;
let v = getRef();
test(v);
Additional information about the issue
This affects the change in React's types between version 18 and 19, where the RefObject type changed current from T | null to only T. The uses of RefObject should add null instead, e.g. RefObject<HTMLDivElement | null>. Making libraries compatible with both the React 18 and React 19 types is difficult due to this TS issue. If a library updates its implementation to React 19, including RefObject<HTMLElement | null> in its definitions, consumers still using React 18 types will see TS errors.
cc. @eps1lon in case you know about this
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
Inizia eseguendo le riproduzioni collegate di TypeScript Playground e confronta i casi RefObject e RefObject2. Traccia il percorso del controllo dei tipi per l'istanza generica e l'assegnabilità strutturale; l'issue non indica file o test del repository. Il lavoro è completato quando i tipi delle proprietà sostituiti equivalenti vengono accettati senza compromettere il comportamento esistente del controllo generico.
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
- 35/100