microsoft / microsoft/TypeScript
Intersected type variable with a recursive constraint fails to infer
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
intersection type variable parameter inference recursive constraint
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type StateConfig<TAction extends string> = {
entry?: TAction
states?: Record<string, StateConfig<TAction>>;
};
type StateSchema = {
states?: Record<string, StateSchema>;
};
declare function createMachine<
TAction extends string,
TConfig extends StateSchema,
>(config: TConfig & StateConfig<TAction>): [TAction, TConfig];
const inferredParams = createMachine({
entry: "foo",
states: {
a: {
entry: "bar",
},
},
});
inferredParams; // ["foo" | "bar", StateSchema]
🙁 Actual behavior
TConfig gets inferred as its constraint
🙂 Expected behavior
I'd expect TConfig to infer successfully
Additional information about the issue
This is very related to the other issue I just created (see here). The underlying problem of this particular example is the same. The inferredType for TConfig fails the compareTypes check in getInferredType here. This time there is no excess property errors apparent to the user because the excess properties are satisfied through the intersection.
Despite the problem having the same root cause I think it's worth discussing this separately. The other issue could be closed as design limitation/working as intended but this one feels to me a lot closer to being an actual bug that would be worth fixing.
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 il risultato dell’inferenza dal link TypeScript Playground fornito, quindi leggi src/compiler/checker.ts intorno a getInferredType e al relativo controllo compareTypes. Ricostruisci il motivo per cui il vincolo ricorsivo intersecato fa ricadere TConfig su StateSchema; il lavoro è completato quando l’esempio inferisce correttamente la configurazione senza introdurre regressioni nel comportamento di inferenza correlato.
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