microsoft / microsoft/TypeScript
Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 Search Terms
object with all context-sensitive properties, self referencial types, xstate
🕗 Version & Regression Information
Tested with TypeScript 7.0.2, the issue exists even in versions as old as 4.4.4
⏯ Playground Link
💻 Code
// problem: doesn't infer D correctly, context and state are any
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? any
state
// ^? any
}
}
}
})
// solution: add a useless non-context-sensitive property `_: null`
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? { foo: number }
state
// ^? "a"
},
_: null
}
}
})
declare const createMachine:
<D extends {
initial: keyof D["states"],
context: object,
states: {
[S in keyof D["states"]]: {
entry?: (context: D["context"], state: S) => void,
_?: null
}
}
}>
(definition: IdentityObject<D>) =>
D
type Identity<T> =
T extends any
? ( T extends (...a: never) => unknown ? T :
T extends object ? IdentityObject<T> :
T
)
: never
type IdentityObject<T> =
{ [K in keyof T]: Identity<T[K]> }
🙁 Actual behavior
The first createMachine invocation doesn't infer the type parameter correctly (it requires at least one non-context-sensitive property) and hence context and state parameters of entry are not inferred.
🙂 Expected behavior
The first createMachine invocation should infer the type parameter correctly without having to pass in a non-context-sensitive property leading to inference of context and state parameters of entry
Additional information about the issue
The premise is adding (or rather not adding) an extra useless property shouldn't make a difference to the inference
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
Beginne damit, den Inferenzunterschied im verknüpften TypeScript Playground anhand der beiden createMachine-Beispiele zu reproduzieren. Lies die generische Deklaration und den IdentityObject-Typ im Issue und verfolge anschließend die Inferenz der Typparameter durch den Compiler für die kontextabhängige entry-Eigenschaft. Als erledigt gilt dies, wenn die Version ohne die zusätzliche _-Eigenschaft context als { foo: number } und state als "a" inferiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100