microsoft / microsoft/TypeScript

Using types from private class properties results in `any` types in `.d.ts` files

Aperta
#60,230 4 commenti 2 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 30/10/2024.

Bug Domain: Declaration Emit
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.