microsoft / microsoft/TypeScript

Object properties are inferred in the wrong order: should infer properties with `NoInfer<T>` AFTER properties with `T`

Abierto
#63,378 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: check: Type Inference Possible Improvement
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

### 🔎 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: ...
});
```

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el Playground enlazado y el ejemplo reducido de fooArgs/foo, variando el orden de las propiedades del objeto y la anotación del parámetro del callback. Compara el comportamiento con el issue relacionado #56297 mientras rastreas cómo se infieren las propiedades NoInfer. Se considera terminado cuando el ejemplo infiere T de forma consistente a partir de las propiedades que contienen T antes que de las propiedades NoInfer, y se ha solucionado el comportamiento dependiente del orden reportado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.