microsoft / microsoft/TypeScript

Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work

Aperta
#64,251 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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

https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEAcCcHsCMBsCmBbAXKAJtRBnAdgOQAuoAlngGaKSgAioAxtJJIg0fAJ4A0j0eRRAA8SAQzwZQOIqMGhRreXk4AoBq1mIAsqIYALcogAUAbxWgyeUkVKj46AESiH3c3wHCi6E6ArRo6ACMoAC+rhbSmjjebhaiMRaJoIgCkJzoRkweIryRggCUoAC8AHygZklJWYIisZUgoAB6APx1SXmIbYkNLV0hdf0W-SH5KioNONDwAK42-OiiGJKioNM4iEg4OKB4-AC01Z5763g41qQAbogQMODURJygAAYA+uh40-DwT6BqGoI6fSGUxucjnOyOZzhdw1LzlXz+IKhaEdaLlOrxdGVZKpdKgTL8WG5GQFYplCrYmGeLoWHqtSlSEmdBl0vrQpJvHafeADNzDUYqDBseAKa5ZaSMf7aXQGPCIVBuAA89E8KQw2wpFjBNghoAA1ohONAKHQANoOVEOAC67KpInQcAAVmwiLbUQlKqaAMqWfWG41mi1MnDWq0e7EpIhpZoZQ72wNxojW4madBewqlUAXaCkDC2jkxrlfPq8oYlOpGIUUbWkeagACSQoE1k4AHlYM72MqShny9jaGMHncG02bA9FQAVMpFNwT5IiNXbcSqJLNfGgOeqiTbIwAOn3mLlV0gvdWeD1uwA7nhQGu5wqGZuF9vQE6XbeR5GW+3O0RJ2UH0pCculGJJ3kQY9B04YdGy-B4fxdf9ijcHxTQAaV9A0jRNCcw0-ZtxwndCrTKfogAOfEUiAA

### 💻 Code

```ts
// 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:
void,
_?: null
}
}
}>
(definition: IdentityObject) =>
D

type Identity =
T extends any
? ( T extends (...a: never) => unknown ? T :
T extends object ? IdentityObject :
T
)
: never

type IdentityObject =
{ [K in keyof T]: Identity }
```

### 🙁 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la differenza di inferenza nel TypeScript Playground collegato usando i due esempi di createMachine. Leggi la dichiarazione generica e il tipo IdentityObject nell’issue, quindi segui l’inferenza dei parametri di tipo del compilatore per la proprietà entry sensibile al contesto. Il lavoro è completato quando la versione senza la proprietà _ aggiuntiva inferisce context come { foo: number } e state come "a".

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.