microsoft / microsoft/TypeScript

Discrepancy when typeToString when enclosingDeclaration is undefined vs defined.

Aperta
#43,988 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I'm using typeToString functionality from the typescript compiler (via ts-morph) to build auto-generated API documentation. I've run into an issue with certain types that use default generic types. My goals:

  • I don't want the default parameters to show up
  • I want the module/sourcefile import included

If I pass enclosingDeclaration, the default parameters don't show up, but the import is gone. If I don't pass enclosingDeclaration the opposite happens - import is there, but the default parameters show up.

🔎 Search Terms

Typescript typeToString generic optional default parameter ignore enclosingDeclaration

🕗 Version & Regression Information

I'm not sure this is bug but there is nothing I could find about it anywhere else.

⏯ Playground Link

link

Code sandbox which shows the issue.

Screen Shot 2021-05-06 at 8 10 57 PM
💻 Code

Types:

export interface WithDefaultGenerics<A = string, B = number> {
  irrelevant: string;
}

export type Test = () => WithDefaultGenerics;
    const text = compiler.typeToString(
      compilerType, // This is the type of `Test`
      undefined,
      TypeFormatFlags.UseFullyQualifiedType | TypeFormatFlags.InTypeAlias
    );
    console.log('text is ', text);

    const text2 = compiler.typeToString(
      compilerType, // This is the type of `Test`
      node.compilerNode, // This is the node container of `Test`
      TypeFormatFlags.UseFullyQualifiedType | TypeFormatFlags.InTypeAlias
    );
    console.log('text2 is ', text2);
🙁 Actual behavior
text is  () => import("/Users/path/to/file").WithDefaultGenerics<string, number>

text2 is  () => WithDefaultGenerics
🙂 Expected behavior

I'm trying to get () => import("/Users/path/to/file").WithDefaultGenerics

The reason this is important to me is because in conjunction with, I suspect, this issue, getting a type for (props: AllCasesProps) => ReactElement<AllCasesProps> ends up looking like this:

Screen Shot 2021-05-06 at 8 27 16 PM

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 il link Playground e le chiamate compiler typeToString nel report, confrontando l’output quando enclosingDeclaration non è definito con quello quando è node.compilerNode. Analizza in che modo TypeFormatFlags.UseFullyQualifiedType e InTypeAlias influenzano gli argomenti generici predefiniti e i nomi qualificati dal modulo. Il lavoro è completo quando viene prodotto il tipo qualificato richiesto senza mostrare i parametri predefiniti, con una copertura di regressione per entrambi i casi.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
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.