microsoft / microsoft/TypeScript

Inline *_base declaration in d.ts files

Aperta
#59,550 5 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experimentation Needed Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

Anonymous class, _base, base, d.ts, api-extractor, recursive

✅ Viability Checklist
⭐ Suggestion

Currently classes which extends anonomous classes compile to d.ts files with two declarations:

class A extends class {} {}

Generates:

declare const A_base: {
    new (): {};
};
declare class A extends A_base {
}

playground link

My suggestion is for it to generate something like this instead:

declare class A extends ({} as {
    new (): {};
}) {
}

More generally replace:

declare const A_base: DEFINITION_OF_BASE_HERE;
declare class A extends A_base {
}

with:

declare class A extends ({} as DEFINITION_OF_BASE_HERE) {
}
📃 Motivating Example

When exporting classes with anonymous bases (for example classes with bases generated using functions, for example in fluid-framework's tree schema system, it would be nice if the generated d.ts file better matched the original source, so tools (like TypeScript and API-Extractor) behave more similarly to how they would if run on the source instead of on the d.ts. This would make the d.ts better serve as a concise summary of the type information of the original code.

💻 Use Cases
  1. What do you want to use this for?
    I know of two cases this would help:

    1. API-Extractor: it would fix https://github.com/microsoft/rushstack/issues/4429 by removing this odd case from d.ts files.
    2. Some recursive types compile without error when the base in inline, but not when its split into a separate variable. Sometimes this even ends up being compilation order dependent (similar to https://github.com/microsoft/TypeScript/issues/55758 ) and can result in failing incremental builds and working clean builds. Making the d.ts declare it inline when the original source files does helps make the d.ts more aligned with the non-d.ts type checking for recursive types avoiding introducing additional such issues specific to the d.ts. I haven't extracted a minimal repro for this, but there are some not so minimal examples (and a workaround) in https://github.com/microsoft/FluidFramework/pull/22122.
  2. What shortcomings exist with current approaches?
    Currently the behavior is confusing since most TS developers don't think about the differences between the d.ts files and the original source, so having to do workarounds to make them stay aligned (like manually exporting the base to make API-Extractor happy, or ensuring that if the base is not inline, that the code still type checked for the recursive case) is confusing and unintuitive.

  3. What workarounds are you using in the meantime?
    For API extractor, manually export the base type under a different name.
    For the recursive type issue, export carefully crafted usage before the declaration which happens to cause it to compile with the split declaration.

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 con l’esempio collegato di TypeScript Playground e con l’output della dichiarazione mostrato nell’issue. Analizza come vengono rappresentate le basi di classi anonime nei file d.ts generati, quindi verifica che la base venga incorporata inline mentre i casi d’uso di API-Extractor e recursive-type rimangano allineati al codice sorgente.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.