Why does my TypeScript sometimes give me an instance and other times give me a class?

I am writing an application to learn TypeScript. It works fine in Chrome, but I have a problem when working in Edge. I have this method:

set position(pos: Point) { const diffAsPoint = pos.minus(this.canvasPos); let diff: Vector2D = diffAsPoint.toVector2D(); // <--- this line if (diff instanceof Vector2D) { console.info("is a vector!"); } else { console.info("not a vector!"); } 

I see that sometimes diff IS a Vector2D and not an instance of Vector2D . Obviously, when this happens, any operation on it leads to Object doesn't support property or method ...

a debugger showing a class, not an instance

The toVector2D method toVector2D simple:

 toVector2D(): Vector2D { return new Vector2D(this.x, this.y); } 

I don't know if this matters, but here is some background:

  • application - a game that runs in the game loop (60 frames per second - using window.requestAnimationFrame(() => this.animloop()); )

  • application works fine in Chrome

  • it doesn’t work fine in IE (it looks like another problem, but I couldn’t see what else when IE and debuggers work during the investigation)

  • uses the latest TypeScript (2.2.1)

  • this happens at a random point after starting, sometimes 2 seconds, sometimes 3 seconds

  • there are other places in the code where this happens, everything related to Point and Vector2D seems to be related to creating them in each frame (some problems disappear if I enter a field, rather than creating every frame)

  • I use AMD and requirejs

UPDATE -------

Using Edge and debugging tools and the “Always update from server” setting when downloading from a website ( http://pacmanweb.azurewebsites.net/ ), I see that the game is trying to run before all modules are loaded . If in Chrome it seems that everything loads before the game starts.

Any ideas?

+6
source share
1 answer

The application does not work for me, because it looks like you are using the default parameter values ​​that are left in the passed code, as it is:

  reset(isDemoMode = false) { 

This ES6 feature does not seem to be supported by Edge, while Chrome most likely does.

tsc should automatically transfer this to ES5. You should check your configuration, make sure that it is not configured to output es6 code.

For instance:

 function obj(x = 5) { } 

should translate to:

 function obj(x) { if (x === void 0) { x = 5; } } 

And a link to the playground .

+2
source

Source: https://habr.com/ru/post/1015514/


All Articles