microsoft / microsoft/TypeScript

Intersected type variable with a recursive constraint fails to infer

Abierto
#55,645 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Proposal Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔎 Search Terms

intersection type variable parameter inference recursive constraint

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230906#code/C4TwDgpgBAysCGwIGED2A7AZgSwOYB4AVAQQGNhsMoIAPJdAEwGcongAnbdXAPigF4oAbwBQUaug4gA-AC4oJcpXRjWCJEzlQAShFKp2DfG07cANLHUoMOAoooYePANwiAvq5GhIlxBBikABYQALbwAsKqbH6a8rr6hsYcXLgWcH4BwWEu7p4MegA28OzQmACu6EpUpCV+ALLwQVwQ+Kr2ytR0EIwsJilmbWhYeJ30zL5ImaHwAzwAFPrDuPKEQ7ZQAGQT1ktEZA7oPACU8gDa7RgWqzZ4ALqei2xQXJgQ7CUMAArF8CEsgjUIPVGoFmnNROJulJ5AAiTCoVAwgbiaIaeQQ8RQeDo1SYiTQqAwgBGxSRuKgbmRFIGbiOnhebw+33YvyYrgA9OyoAA9aQiIA

💻 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.

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

Reproduce el resultado de inferencia a partir del enlace proporcionado de TypeScript Playground y, después, lee src/compiler/checker.ts alrededor de getInferredType y su comprobación compareTypes. Sigue el motivo por el que la restricción recursiva intersecada hace que TConfig recurra a StateSchema; se considera terminado cuando el ejemplo infiere correctamente la configuración sin provocar regresiones en el comportamiento de inferencia relacionado.

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
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.