Skip to content

Using types from private class properties results in any types in .d.ts filesΒ #60230

Open
@bradzacher

Description

@bradzacher

πŸ”Ž Search Terms

class private any index access

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBAYwDYEMDOa4DEITgbwCg4S4woBLANxRmDgH1yIxhYBPALjjRkoDsA5nAC8cAOTiA3MVIII-XlACuCGNAAUKKIO44IAbXFMoLNjHbiAugEoCskgF9CzoA

πŸ’» Code

export class Foo {
    private _property: string = '';
    constructor(arg: Foo['_property']) {
    }
}

πŸ™ Actual behavior

Generated .d.ts is:

export declare class Foo {
    private _property;
    constructor(arg: Foo['_property']);
}

πŸ™‚ Expected behavior

Generated .d.ts is:

export declare class Foo {
    private _property: string;
    constructor(arg: Foo['_property']);
}
// or
export declare class Foo {
    private _property;
    constructor(arg: string);
}
// or
/* Typescript Errors on the code to tell you it's going to generate an `any` */

Additional information about the issue

This behaviour is problematic because it creates a desync between consumers of .ts files and consumers of .d.ts files for the same code.

For example:

new Foo(1);

If the Foo type comes from the .ts file, then TS will error on this code as it can see that the argument type is string.
OTOH if the Foo type comes from the .d.ts file, then TS will NOT error on this code as it sees the argument type as any.


We have just uncovered this at Canva.
A user reported an error showing up in their IDE against our master branch (i.e. code that has passed CI as typechecked).
The code is structured such that the file with the error (A) is in a separate project to the file declaring the class (B).
This means that we have the exact scenario above where (A) consumes (B)'s .d.ts during our CLI builds, but (A) consume's (B)'s .ts within the IDE.

This pattern of declaring a type based on a private property's type is quite pervasive across our codebase and it's surprising that this is the first problem that's been actively revealed.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions