microsoft / microsoft/TypeScript
Object properties are inferred in the wrong order: should infer properties with `NoInfer<T>` AFTER properties with `T`
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
### 🔎 Search Terms
NoInfer
brittle inference
object properties
Related issues:
- https://github.com/microsoft/TypeScript/issues/56297 : inference depends on the order of the properties. This explains the inconsistency in my issue.
### 🕗 Version & Regression Information
Tested in 6.0.2 and nightly.
### ⏯ Playground Link
[Playground Link](https://www.typescriptlang.org/play/?noUnusedLocals=true&noUnusedParameters=true&exactOptionalPropertyTypes=true&noPropertyAccessFromIndexSignature=false&ts=6.0.0-dev.20260401#code/KYDwDg9gTgLgBDAnmYcASEIGsDOd8C8cASsAMbQAmAPDjFAJYB2A5gDRwAUAdLwIZQWOAFxw+TRAG0AugEo4BAHxiJigNwAoDUhRwA6gxgALDNhzUAKnFAxgTSnlO5lRAN5HMuURYC+mnagA4sAwTuZWNnYO+oYmnubiiIqKGviEcBEgtvZ4BsZh1MwAZsBQ6MoA-OhwokzAAG6lanBaAejxAExo4pQANqXmaNZZUY7xLnCuqWmSANJwzHBYwIgQRdV8eAAGACSuswBkdIysPlvSomhz0tP4APR3C+sA7qhQwAC2EI1ieEpwlAgcCYEHgz2gWBUiC+724Gj8WlAkFgCyYtigRT4ZFQAGEIGioBBev0oJZhtlomFlFM0nAPGZvJofBoyL1NngAFqTW50+IKSZFTCiTgAfQYogAzAAWWT-Vw+BHM7TIXH4+hEkkABUJ9QYlFK1BxAHkAHIWYhGgAyloAosRyaM4HiCRqDYlkvzODzaT7fX7fTABCwQpcLABZW39D52eAPJ5wV5wd5fH6GBZgiE4OH+nO5uDyf7Gs0W612xHgaDwNoANQYwGeADEsTBoIgAIKCcxF81W232yI5J1qwnEt2qCY0v0UF2jqDa7560qiZ3q2fz3X60ndkt9xRsHk+OAHbnetJ8GCBshGFcjkkVYWnvN+uMMF5vT7fVCGDiA4A4JgAOQZlAWDZk+ebTquJKiCaEAAJJMCUW6mj2pbECk4F+gWyj1BAer7rSh7HpqAgwAwfC9NQwShD0JI4A2hIfNQsEIUhhooTudrJBhaQvm+SYfj8JGwORlHKMYdgLPydSNGUnB6BYDYVLIrQqnA1HdPYdEMRATHbr2doOoON6uqS7o8ekYRdLRAxUSEBT6WhySaLSfEJqgACuOBBPZ8TscWBnoQoygMPyiRcApSkqRoSKVgCwCYh5vTwEUHlMGQZH4nAZDvOewC1vWOJsjg5iPphaSOX2RnRCZs7UO6ZVPooXpPiKQYiHABWNs2rYdkI-mobuZXyPKWh3AAVONdxaDlwB5V1RXspwk73I8r7uXAYAefAaZ8EU6JiBeWLXsOpmcLIHBpr+-5AQmEJgatd0gXg62Jt58BIr0DBkIYvSIAgRioG0axwCKZAPdlp1rjqi5QMKYPYcC9ZwBy537jybmJsmn7pnA0CbdtuN7QdgoQGjuPXYBwGgTy56XidM4kqKABeI0+ARaSk6KDBs-CshqEAA)
### 💻 Code
See playground for the full example.
```ts
type fooArgs = {
a: (_: A) => X,
// note: NoInfer> better than Y>, doesn't change this issue though
b: NoInfer>
}
function foo(args: fooArgs) {}
foo({
a: (_) => ...,
b: ... // inference of the value given to b is VERY brittle.
});
```
### 🙁 Actual behavior
Sometimes, `b` is inferred before `a`.
Then, as `a` is the one used to infer the generic parameter `T`, the value given to `b` doesn't have the correct type (because inferred before `a`).
This is likely due to the fact that the value given to `a` is a callback with a parameter we didn't specify the type (`(_) => ...`). Therefore, TS has first to look at `fooArgs` to infer the callback's parameter type (i.e. the type of `_`).
Due to that, `b` is sometime inferred before `a` has been properly inferred. This behavior depends on the order of the object properties (cf related issue). It is also influenced by other defined properties, and how they are defined. Making the inference quite chaotic.
### 🙂 Expected behavior
TS should infer `NoInfer` properties AFTER `T` properties, in order to prevent such issues.
### Additional information about the issue
The fact that the order of the inference changes is quite troublesome, hiding the issue in some cases.
Could be nice to have a kind of tool/flag to detect such kind of potential issues, and to help debugging.
_EDIT:_ A possible workaround:
```ts
type fooArgs = {
b: Y
}
function foo(a: (_: A) => X, args: NoInfer>) {}
foo(
a: (_) => ..., {
b: ...
});
```
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par le Playground lié et l’exemple réduit fooArgs/foo, en faisant varier l’ordre des propriétés de l’objet et l’annotation du paramètre du callback. Comparez le comportement avec l’issue associée #56297 tout en suivant la manière dont les propriétés NoInfer sont inférées. Le travail est terminé lorsque l’exemple infère systématiquement T à partir des propriétés portant T avant les propriétés NoInfer, et que le comportement dépendant de l’ordre signalé est corrigé.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100