Skip to content
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

this.constructor.staticMethod() is ok in JS, but is an error in TS #59052

Closed
vadzim opened this issue Jun 27, 2024 · 1 comment
Closed

this.constructor.staticMethod() is ok in JS, but is an error in TS #59052

vadzim opened this issue Jun 27, 2024 · 1 comment

Comments

@vadzim
Copy link

vadzim commented Jun 27, 2024

πŸ”Ž Search Terms

constructor class type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEDC5WgbwFAEgIBcyYJbGgGVs9gBZAU0wAsB7AEwAoBKZdNYWgOwlpAoB0IWgHNGAIiw580XDABmuLhXHN0AX3SUaDFmzRoA9Ieg050AO60ATgGsYS6AAdwjgFKEANNABGAV0xZGE4eXHoKawp6aEgYrmgI6xtZeIAVQnYzCAEQrGs-YEwbAWJpcio6JjU0TU0UZQs4BAgWAW1KliA

πŸ’» Code

class Class {
	static StaticMethod() {
		console.log("static is fine")
	}
	Method() {
		// this works in plain JS, but is considered as an error in TS
		this.constructor.StaticMethod()
	}
}

new Class().Method()

πŸ™ Actual behavior

TS emits an error Property 'StaticMethod' does not exist on type 'Function'. ts(2339)

πŸ™‚ Expected behavior

no error in TS

Additional information about the issue

this also works well in JS:

class Class {
	static StaticMethod() {
		console.log("Class static")
	}
	Method() {
		// this works in plain JS, but is considered as an error in TS
        // prints "Derived static"
		this.constructor.StaticMethod()
	}
}

class Derived extends Class {
    static StaticMethod() {
        console.log("Derived static")
    }
}

new Derived().Method()

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEDC5WgbwFAEgIBcyYJbGgGVs9gBZAU0wAsB7AEwAoBKZdNYWgOwlpAoB0IWgHNGAIniQYWHPnHN0AX3SUaDFmzRoA9Dug1cMAO60ATgGsYuLtAAO4G9ABShADTQARgFdM0I9CcPLj0FGYU9NCQUbZhZub+tgAqhCjQ6RnpevZmNpgw4gAiYbgAbhHQsqTi7IYQAkFYZt7AmOYCxHLkVHRMimgqKiig0tDFueWRFAAemBRc9DBSiKiZVfhEJPhqvZqrmRmNfILCYkUlk5VbwAppGYMoKFwUxmMXESwCOxrMQA

@MartinJohns
Copy link
Contributor

Duplicate of #3841 / #32452.

@vadzim vadzim closed this as completed Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants