Skip to content

[3.7.0] Generic class with private property generates d.ts file missing type on generic property #33877

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

Closed
fearthecowboy opened this issue Oct 8, 2019 · 6 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@fearthecowboy
Copy link

TypeScript Version: 3.7.0-beta (and just tested with typescript@next)

Search Terms: private field declaration

Given a class:

class foo<T> {
    private get value() : T {
        return this.v;
    }   
    constructor (private v:T) {
    }
}

Actual behavior:

running:
.\node_modules\.bin\tsc .\foo.ts --target es2016 --declaration

you will get a foo.d.ts file that looks like this:

declare class foo<T> {
    private v;
    private get value();
    constructor(v: T);
}

Dependent projects consuming that d.ts file will break, because the generic type isn't propagated to the private field declaration.

node_modules/foo/index.d.ts:3:17 - error TS7033: Property 'value' implicitly has type 'any', because its get accessor lacks a return type annotation.           
     private get value();                                                                                                                                                        ~~~~~           

Expected behavior:

declare class foo<T> {
    private v: T;
    private get value() : T;
    constructor(v: T);
}

Playground Link: playground doesn't generate d.ts file... (grrr!)

Related Issues: no.

@nmain
Copy link

nmain commented Oct 8, 2019

Shouldn't the expected behavior be not to change the emit, but to remove the error? private members are only emitted in declarations to guard against name collisions, and this doesn't produce an error even though v is untyped:

declare class C { private v }

@fearthecowboy
Copy link
Author

O_o

Given that .d.ts files are typing info, if the error was removed (in 3.7+) that would mean that consumers of the API that didn't have the latest typescript would break.

That doesn't quite seem right.

@nmain
Copy link

nmain commented Oct 8, 2019

The alternative is obligating declarations to expose more information about private members than is required. Maybe the emit could be changed to do this, regardless of the type of f:

declare class C { private get f(): never }

That would maintain backwards compatibility.

@fearthecowboy
Copy link
Author

That seems reasonable. never was introduced in what, 2.0?

I'm ok with that.

The tangential question is ... what is the expectation for d.ts files and consumability with prior compiler versions.

@ajafff
Copy link
Contributor

ajafff commented Oct 9, 2019

The error when consuming such declaration files will be fixed by #33896

@sandersn
Copy link
Member

The error should just be removed. Accessors were not allowed in d.ts files until 3.6 anyway, so it is not possible to make them compatible with anything 3.5 or below.

@ajaff Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants