Skip to content

Missing explicit return type for static methods using private static methods causes return type inference issues in declaration files #61545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iSwitchL opened this issue Apr 7, 2025 · 1 comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@iSwitchL
Copy link

iSwitchL commented Apr 7, 2025

🔎 Search Terms

"javascript private static methods return type inference", "private static method inference error in declaration file"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about JavaScript private static methods return type inference.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250406#code/MYGwhgzhAEBiD29oG8BQrqehALmHAlsNAMQBGYATgBQCUK0AvultAA4CuZIR2ehxCjXppWrSgFMcHSgDs4iAHTkqAbgxZmqRkA

💻 Code

foo.ts:

class Foo {

    static #bar() { }

    public static bar() {
        return Foo.#bar;
    }

}

foo.d.ts:

declare class Foo {
    #private;
    static bar(): typeof Foo.__#1@#bar;
}

🙁 Actual behavior

The foo.ts file will compile successfully without errors, but the generated foo.d.ts file will be incorrect because it contains a syntax error (Foo.__#1@#bar, ts(1005)).

As a result, any TypeScript subproject that depends on this compiled code will fail at compile time due to the invalid syntax in the foo.d.ts file.

🙂 Expected behavior

Enforce an explicit return type for the public static bar() method, as the presence of it currently allows bypassing this issue.
Otherwise, adjust how TypeScript infers the return types of JavaScript's private static methods.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Apr 8, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 8, 2025
@Teruk15
Copy link

Teruk15 commented Apr 12, 2025

Hi!! may I work on this?

iSwitchL added a commit to lucyus/actionify that referenced this issue Apr 12, 2025
…stener' to resolve TypeScript type inference issue

TypeScript doesn't correctly compile types of true
private fields when infering type.
See: microsoft/TypeScript#61545
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants