microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.33k stars 12.54k forks source link

tsc with allowJs reports TS9005 error for nested anonymous constructor functions #57523

Open brian6932 opened 9 months ago

brian6932 commented 9 months ago

🔎 Search Terms

TS9005: Declaration emit for this file requires using private name '(Anonymous function)'. An explicit type annotation may unblock declaration emit.

🕗 Version & Regression Information

Related to #55172 When 2 separate nested this bindings are used within constructor functions, the following error's returned always at the top of the file (this was really annoying to debug 😅)

Declaration emit for this file requires using private name '(Anonymous function)'. An explicit type annotation may unblock declaration emit.

⏯ Playground Link

https://tsplay.dev/NV57Gw

💻 Code

const a = new function () {
    this.b = new function () {
        this.c = 1
    }
}

🙁 Actual behavior

Complains

🙂 Expected behavior

Shouldn't complain

Additional information about the issue

No response

rubiesonthesky commented 9 months ago

Last typescript version in playground that does not report error is 3.6.2 so I don't think this changed in the linked PR.

In the linked issue https://github.com/microsoft/TypeScript/issues/55172, weswigham notes that versions 3.6.6 do not show error that the code there is creating, so this probably is not working earlier versions either.

Andarist commented 9 months ago

I'm not sure if this is meant to compile successfully but, if I'm reading this correctly, it shows an extra thing that is a bug. If you comment out the nested assignment to the this.c property then you might see this emit:

declare function a(): void;
declare class a {
    b: any;
}

This doesn't seem right because a is not a class - it's an instance of a class. Extra TS playground that shows the difference between anonymous and named functions here: TS playground

And an extra thing that I observed here, private name for #prop can leak in the declaration emit - this shouldn't happen (TS playground):

// input

// @target: esnext
function test() {
  return class A {
    #prop = 10;
  };
}

export const a = new (test())();

// dts
export declare const a: {
    "__#12@#prop": number;
};
fatcerberus commented 9 months ago

@Andarist Re: Private name leakage - https://github.com/microsoft/TypeScript/issues/56145 -> https://github.com/microsoft/TypeScript/issues/36548

kopax commented 5 months ago

Hello, any update on this ?

Using yarn4 and prisma, we get stuck as :

This makes me totally blocked and forced to downgrade yarn :(.

Any workaround exisiting ?

Jeangowhy commented 3 weeks ago

Can someone tell what is this problem?