Skip to content

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

Open
@brian6932

Description

@brian6932

🔎 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

Activity

rubiesonthesky

rubiesonthesky commented on Feb 25, 2024

@rubiesonthesky

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 #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

Andarist commented on Feb 25, 2024

@Andarist
Contributor

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

fatcerberus commented on Feb 26, 2024

@fatcerberus

@Andarist Re: Private name leakage - #56145 -> #36548

added
Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
on Feb 26, 2024
added this to the Backlog milestone on Feb 26, 2024
kopax

kopax commented on Jul 4, 2024

@kopax

Hello, any update on this ?

Using yarn4 and prisma, we get stuck as :

  • yarn4 can't generate prisma client in node_modules, so it is generated in the workspace with my files
  • I can't use annotation as this is often re-generated code

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

Any workaround exisiting ?

Jeangowhy

Jeangowhy commented on Nov 8, 2024

@Jeangowhy

Can someone tell what is this problem?

seahindeniz

seahindeniz commented on Apr 20, 2025

@seahindeniz

In my case, "composite": true, was in my tsconfig.json file. Removing it solved the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kopax@rubiesonthesky@fatcerberus@seahindeniz@RyanCavanaugh

        Issue actions

          tsc with allowJs reports TS9005 error for nested anonymous constructor functions · Issue #57523 · microsoft/TypeScript