Giter VIP home page Giter VIP logo

Comments (13)

Andarist avatar Andarist commented on May 1, 2024 2

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.

MartinJohns avatar MartinJohns commented on May 1, 2024 1

this in a static context is valid and refers to the static side of the class. The issue is that the constructor is typed as non-specific Function, which is a design limitation. There's an open issue #3841 for this. edit: This was not correct. See Andarist response.

from typescript.

Andarist avatar Andarist commented on May 1, 2024 1

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.

LeOndaz avatar LeOndaz commented on May 1, 2024

TS versions tested and reproduce the same bug:

  • 5.2.2
  • 5.4.5

from typescript.

LeOndaz avatar LeOndaz commented on May 1, 2024

@MartinJohns does this mean that it should work, but un-typed ?

from typescript.

LeOndaz avatar LeOndaz commented on May 1, 2024

@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.

LeOndaz avatar LeOndaz commented on May 1, 2024

Here's an example of the error:
image

from typescript.

MartinJohns avatar MartinJohns commented on May 1, 2024

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.

LeOndaz avatar LeOndaz commented on May 1, 2024

@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.

LeOndaz avatar LeOndaz commented on May 1, 2024

@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.

LeOndaz avatar LeOndaz commented on May 1, 2024

@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.

RyanCavanaugh avatar RyanCavanaugh commented on May 1, 2024

TypeScript doesn't validate code that is, for all intents and purposes, inside an eval call

from typescript.

typescript-bot avatar typescript-bot commented on May 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.