microsoft / microsoft/TypeScript

Resolution order dependent circular heritage

Abierto
#61,711 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: check: Type Circularity Help Wanted 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

resolution order dependent circular heritage, circular base type, circular mixin

🕗 Version & Regression Information
  • This changed in #39675

However, it appears this PR was the first time that circular heritage was (consistently) reported. Therefore this issue has essentially always existed.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/PTAEDEEsA8FMBNSQGagJ4HsCuoDGGBbA2AOwBdQyALSAZ1ABtITZRsKMAnUWgd0jK4qlDKAAGAQVxkuABQAqAJQBMsMQDoAUPFi4GAQ06t8JWhWgAuUAElTZfSVyx5aAA6wAPGTewMqKTKcCiqwAHwA3JpRIKAAqrQIIjxUGLyUVPocJAxooPqgOu4kOo65GCTi8mJI9CxOtLSGuchc6caQnLhYBpwCaFoxEvTMADRtoMjM+gygRrQYDABuiYvTWKzwGLC1GBQEmWSwnFre7qAAIrBF8LQA8iQe8qGgALx5JGiR2ro9xgYNoACrQA3ppQKBXJwMK4rJdrncHgFIMsAKLIZC6MgAbQARJDoTiALoRTQAXyiOj0hlYJH0xForn0ThshwIoFB4JiUDgiBQ6GwPH4gmE1DoSQcSHIR2QTNgWnBp1Y1gIrgYsGI5EykHKrxsdgcThc7g85wwXQ1ZAAwv9aJbypMAOa46ysokk8nfKlGPA2lnq9lg0CWC5XUg3e4eJGo9GY3H41xuyIerkwRJ8zA4IwEDDLcZiADKWAARka1KAHaQjpBcBDDHTYIdjpoqQCgUElKoPIWSz5dQ40M9YNBDsV6G32cmwNy06gM7N1TnWNRWAXi6XqhWWL0a4zOPXG1oW-QXergp3u6W+x9B8Ow8fWROosxGzLmabzaQrTa7SRHQHOVOqa8rOApCA4FbiIqfiAtIcgdmWMiQT40Ftmenj9qEYiBm2VhQf4sHtiEXwARAQFICBOBgSQEFiHhfoEGh1SIbRyGoCeDHwR4GFYeC7G4ax9FoUmFI-NSPr6K20jIrAaIYtIoBDiONygNakCfu+WAWgAsjAzAABQAEISbAUYyTG0gAJT-hCUIwqAJCaUWRzCZ6vwTFgjhkNqFSqepZqaZ+OnQMwHiBimPLkfyOAMBgGAJBUy54OUZh7s+bCoGIRkJNaEm0BogZZbAOUAopd72bAaR6eo1WGA6tBWP2WKElZLzPBgRYAFaYiMmihIZxlWIVxW0BZg3GcNXyUm5R6gIVpmyZi1kmClWAEVVNWcHVsKhqOEbseoyqquqn5auUoQWUmQA

💻 Code
// Fixed if you comment this line out or switch to `ActorPTR2e`.
declare const x: InstanceType<typeof ActorPTR2e>;


// Used to show that only a dependency on `T` is necessary for the circularity.
// As in, the final resolved value does not matter.
type DependsOn<T> = any;

declare class Actor {
  prop: DependsOn<ActiveEffect["prop"]>;
}

declare namespace Item {
  // Fixed if you switch this to an interface.
  type Implementation = InstanceType<DocumentClassConfig["Item"]>;
}

declare class Item {
  x: DependsOn<ActiveEffect["prop"]>;
}

// Fixed if you remove the `SubType` generic parameter.
class ActorPTR2e<SubType = any> extends Actor {}

// Fixed if you remove the `SubType` generic parameter.
class ItemPTR2e<SubType = any> extends Item {}

interface DocumentClassConfig {
  // Fixed if you change `typeof ActorPTR2e` to `typeof ActorPTR2e<any>`
  Actor: typeof ActorPTR2e;

  // Fixed if you change `typeof ItemPTR2e` to `typeof ItemPTR2e<any>`
  Item: typeof ItemPTR2e;
}

declare class ActiveEffect extends ClientDocumentMixin(BaseActiveEffect) {
  prop: number;
}

declare function ClientDocumentMixin<
  // Fixed if you loosen the constraint of `BaseClass`.
  BaseClass extends new (...args: any[]) => object,
>(Base: BaseClass): BaseClass;

declare class BaseActiveEffect {
  constructor(...args: DependsOn<Item.Implementation>);
}
🙁 Actual behavior

Numerous errors, namely:

Type alias 'Implementation' circularly references itself.
Type 'ActiveEffect' recursively references itself as a base type.
'ActiveEffect' is referenced directly or indirectly in its own base expression.
'args' is referenced directly or indirectly in its own type annotation.
🙂 Expected behavior

No error, as seen when you comment out the line declare const x: InstanceType<typeof ActorPTR2e>;. The error going away indicates that this circularity is resolution order dependent.

I understand how odd the code is out of context but I did actually run into this in a real codebase.

Additional information about the issue

No response

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 la reproducción vinculada de TypeScript Playground y sigue los diagnósticos informados de alias de tipo circulares, tipos base y anotaciones relacionados con InstanceType, la herencia de clases genéricas y las restricciones de mixin. Se considera terminado cuando la reproducción ya no informe de estos errores dependientes del orden de resolución sin cambiar el ejemplo para evitar la circularidad.

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.