Comments (13)
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 đ
from typescript.
this
in a static context is valid and refers to the static side of the class. The issue is that the edit: This was not correct. See Andarist response.constructor
is typed as non-specific Function
, which is a design limitation. There's an open issue #3841 for this.
from typescript.
Within the static
block the this.constructor
doesn't point to your MyClient.prototype.constructor
but to Function
(same as (class Foo {}).constructor
). So the code above is equivalent to this:
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());
from typescript.
TS versions tested and reproduce the same bug:
5.2.2
5.4.5
from typescript.
@MartinJohns does this mean that it should work, but un-typed ?
from typescript.
@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
from typescript.
Here's an example of the error:
from typescript.
on the failure of compilation
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.
from typescript.
@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 bad
javascript file, which means ts compiled it badly, because it didn't know how to handle this case right? this case in typescript's opinion, is fine, but once we get it running, it's not?
from typescript.
@Andarist forgive my lack of knowledge, how is it valid and works but at the same time the compilation results in this weird eval
version, my brain is overheating now
from typescript.
@Andarist got it, when I read what @MartinJohns mentioned, I thought the return type is Function
but it's value points to MyClient.prototype.constructor
.
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
from typescript.
TypeScript doesn't validate code that is, for all intents and purposes, inside an eval
call
from typescript.
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- Optional parameter needs to be provided HOT 1
- Module resolution: Imported files are missing from emit when imported via `package.json#imports` HOT 1
- Error: Debug Failure. False expression. at tryGetImportFromModuleSpecifier
- Feature request: LSP refactor for filling an object literal with zero values HOT 4
- LSP not recommending shortest subpath imports from a shared package in a monorepo HOT 1
- Destructuring object, ability to explicitly tell the variable is unused HOT 3
- Error type on spreading array with additional props HOT 1
- Adding "WebWorker" to the `lib` compiler options causes compilation to fail? HOT 1
- Some of the semantic diagnostics use fileName and not relative file name as part of semantic diagnostics and it makes tsbuildinfo unportable
- Add support for nixos for easily on-boarding developers onto the project. HOT 1
- Clarify logging for "No Project" when hitting memory limit
- æłćç±»ćèȘćšæšćéèŻŻ HOT 1
- Typescript allows assignment of wrong type to property on union type HOT 9
- Wrong inferred initial value in reduce HOT 2
- Type guard does not work with `Readonly` props HOT 1
- Allow --build with Intermediate Errors - shouldn't happen on tsconfig errors HOT 3
- Constraints of nested conditional types applied to constrained type variables are incorrect
- Inferred project that currently use "current directory of tsserver host" needs some special handling
- Unable to use rest on generic that has been narrowed to an object HOT 5
- Error out if using node builtin modules and global variables in browser environment HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
đ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. đđđ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google â€ïž Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.