microsoft / microsoft/TypeScript

Discrepancy when typeToString when enclosingDeclaration is undefined vs defined.

Open
#43,988 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Playground link and the compiler typeToString calls in the report, comparing output when enclosingDeclaration is undefined versus node.compilerNode. Investigate how TypeFormatFlags.UseFullyQualifiedType and InTypeAlias affect default generic arguments and module-qualified names. Done means producing the requested qualified type without showing default parameters, with regression coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.