Skip to content

Declaration emit incorrectly prints uninstantiated generic type #44727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marikaner opened this issue Jun 24, 2021 · 2 comments
Open

Declaration emit incorrectly prints uninstantiated generic type #44727

marikaner opened this issue Jun 24, 2021 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@marikaner
Copy link

marikaner commented Jun 24, 2021

Bug Report

I have a type with an unused generic type parameter.*
This type annotates an optional parameter property in the constructor of a class.
Starting with Version 4.2.X of TypeScript, this class is incorrectly transpiled, so that TS code using the transpiled sources (.d.ts) does not transpile anymore.
The error was:

error TS2304: Cannot find name 'T'

No wonder it cannot be found, it was not in the original code (please see the minimal example below).

During my investigation I found that this issue occurs, when these prerequisites are met:

  1. The given type has an unused generic parameter.
  2. The given type represents a union type.
  3. You have a class with a constructor argument using the given type.
  4. The constructor argument is a parameter property.
  5. The constructor argument is optional.

This list might not be complete or exact.

* (Tried something. Didn't work. Postponed it, but left the parameter for later use.)

🔎 Search Terms

generics, transpilation, missing parameter

🕗 Version & Regression Information

  • This changed between versions 4.1.X and 4.2.X

⏯ Playground Link

Playground link with relevant code

💻 Code

Before transpilation:

type BrokenType<T> = 'a' | 'b';

class MyClass {
    constructor(readonly arg?: BrokenType<any>) {}
}

After transpilation

declare type BrokenType<T> = 'a' | 'b';
declare class MyClass {
    readonly arg?: BrokenType<T> | undefined;
    constructor(arg?: BrokenType<T> | undefined); // <----- T should be any
}

🙁 Actual behavior

The code is transpiled to contain the generic parameter name, instead of the actual type that was passed as argument.

🙂 Expected behavior

I would expect the result to contain the original type as specified in the original code. The generic parameter is not known when consuming the type. This worked before version 4.2.X.

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.4.1 (RC) milestone Jun 24, 2021
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Jun 24, 2021
@RyanCavanaugh RyanCavanaugh changed the title Incorrect transpilation of generic type parameter Declaration emit incorrectly prints uninstantiated generic type Jun 24, 2021
@weswigham
Copy link
Member

Fixed by #42211 - a dupe of #42079 (though, admittedly, a simpler repro)

@jakebailey
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
5 participants