Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm writing an app to learn TypeScript. It works fine in Chrome, but I have a problem when running in Edge. I've got 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'm seeing that, sometimes, diff IS a Vector2D as opposed to being an instance of a Vector2D. Obviously, when this happens, any operation on it results in a Object doesn't support property or method ...

debugger showing class rather than instance

The method toVector2D is simply:

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

I don't know if this makes a difference, but here's some background:

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

  • the app runs fine in Chrome

  • it doesn't run fine in IE (it looks like a different issue, but I haven't been able to see what yet as IE and the Debugger Tools crash when investigating)

  • it uses the latest TypeScript (2.2.1)

  • it seems to happen at a random point after start-up, sometimes 2 seconds, sometimes 3 seconds

  • there are other places in the code where this happens, all related to Point and Vector2D, seemingly related to creating them in every frame (some of the issues go away if I introduce a field rather than creating one every frame)

  • I'm using AMD and requirejs

UPDATE -------

Using Edge and the debug tools and setting 'Always refresh from server', when loading from the web-site (http://pacmanweb.azurewebsites.net/), I can see that the game attempts to run before all the modules are loaded. Whereas in Chrome, it seems to wait for everything to load before running the game.

Any ideas?

like image 766
Steve Dunn Avatar asked Mar 06 '17 08:03

Steve Dunn


People also ask

How do you use this in TypeScript?

The "this" keyword always points to the object that is calling a particular method. The type of "this" in an expression depends on the location in which the reference occurs: In a constructor, member function, or member accessor, this is of the class instance type of the containing class.

Can you use TypeScript and JavaScript together?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).

What is TypeScript example?

TypeScript is an open-source, object-oriented language maintained by Microsoft. Due to the static typing in TS, code written in TypeScript is more predictable and is generally easier to debug than normal JS. Before coding in TypeScript, we need to install it on our local machine.

What is TypeScript language?

TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.


1 Answers

The app fails for me because it looks like you are using default parameter values that are left in the transpiled code as is:

  reset(isDemoMode = false) {

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

tsc should automatically transpile this to ES5. You should check your configuration, make sure you don't have it set to output es6 code.

Eg:

function obj(x = 5) {  
}

should be transpiled to:

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

And a link to the playground.

like image 77
toskv Avatar answered Oct 23 '22 14:10

toskv