-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
static
methods can access this.constructor
without complaints and compiles correctly, but a runtime error shows up
#58210
Comments
TS versions tested and reproduce the same bug:
|
|
@MartinJohns does this mean that it should work, but un-typed ? |
static
methods can access this.constructor
without complaints static
methods can access this.constructor
without complaints but a compilation shows up
@MartinJohns I forgot some crucial details, I've updated this This issue focuses on the failure of compilation, rather than the type error itself, I just revised where I faced this |
But it compiles fine, you just end up with a runtime error. This is to be expected, because your static constructor can not deal with the argument you provide. That TypeScript lets you provide that argument without a compilation error is due to the issue I mentioned before. |
@MartinJohns not sure what I would call it, but isn't a Syntax error a sign that the output of TS is not correct? I understood what you sent, I'm just trying to know what's happening in runtime, it tries to run a |
static
methods can access this.constructor
without complaints but a compilation shows upstatic
methods can access this.constructor
without complaints and compiles correctly, but a runtime error shows up
The syntax error reported here comes from JS - not TS. What @MartinJohns is saying is that this is valid and it works: class MyClient {
static x = 20;
y;
constructor() {
this.y = "This is a demo";
}
static getInstance() {
return this.constructor("return Math.random();");
}
}
const x = new MyClient();
const mathRandom = MyClient.getInstance();
console.log(mathRandom(), mathRandom(), mathRandom()); The problem with your code is that you are essentially trying to compile a function with such body: THIS WON"T COMPILE CORRECTLY And this, in fact, won't compile correctly 😉 |
@Andarist forgive my lack of knowledge, how is it valid and works but at the same time the compilation results in this weird |
Within the class MyClient {
static x = 20;
y;
constructor() {
this.y = "This is a demo";
}
static getInstance() {
// return this.constructor("return Math.random();"); // same thing as the one below
return Function("return Math.random();");
}
}
const x = new MyClient();
const mathRandom = MyClient.getInstance();
console.log(mathRandom(), mathRandom(), mathRandom()); |
@Andarist got it, when I read what @MartinJohns mentioned, I thought the return type is This is why I asked if it's untyped in my reply, all good now, thanks for taking the time to reply and thanks you @MartinJohns as well |
TypeScript doesn't validate code that is, for all intents and purposes, inside an |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
static, static method this, this static
🕗 Version & Regression Information
⏯ Playground Link
https://stackblitz.com/edit/vitejs-vite-cwkeqq?file=src%2Fmain.ts
💻 Code
🙁 Actual behavior
Typescript does not complain, but this does not compile correctly
🙂 Expected behavior
An error because I'm accessing
this
in astatic
method or compile correctly, or bothAdditional information about the issue
This does happen with
this.constructor
but does not happen withthis.y
In other words, accessing
this.y
shows an error.The text was updated successfully, but these errors were encountered: