Description
π 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
π» 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.