microsoft / microsoft/TypeScript
Intersected type variable with a recursive constraint fails to infer
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Inference-Ergebnis über den bereitgestellten TypeScript Playground-Link und lies anschließend in src/compiler/checker.ts im Bereich um getInferredType und dessen compareTypes-Prüfung. Verfolge, warum die geschnittene rekursive Constraint dazu führt, dass TConfig auf StateSchema zurückfällt; als erledigt gilt die Aufgabe, wenn das Beispiel die Konfiguration erfolgreich inferiert, ohne das zugehörige Inference-Verhalten zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100