deeleman / learning-angular2

Code samples repository for the examples provided in "Learning Angular 2", published by Packt Publishing - THE BOOK COVERS UP TO RC.1 and hence is severely OUTDATED. PLEASE REFER TO THE LATEST EDITIONS INSTEAD.
https://www.packtpub.com/web-development/learning-angular-2
108 stars 59 forks source link

Chapter 3 - setInterval does not return a number anymore? #19

Closed vulkanino closed 8 years ago

vulkanino commented 8 years ago

Tried the example at page 72:

class CountdownComponent 
{
    intervalId: number;

    constructor() 
    {
        this.intervalId = setInterval(() => this.tick(), 1000);
    }
}

But the TypeScript compiler in the VisualStudio Code complains that setInterval does not return a number:

[ts] Type 'Timer' is not assignable to type 'number'. (property) CountdownComponent.intervalId: number

I had to modify the id declaration to:

intervalId: NodeJS.Timer;

and it compiles in the IDE but doesn't with tsc. I don't get it, what is the compiler difference between the IDE and the tsc command line?

I've checked the Nodejs docs for setInterval and:

Returns a intervalObject for possible use with clearInterval.

intervalObject is not a number, in fact in the console if I print out the intervalId I get a... ZoneTask?

image

I'm too confused.

deeleman commented 8 years ago

By using NodeJs.Timer you're resourcing on the IDE builtin runtime to perform the transpilation, which probably leverages a more up to date version of the TypeScript compiler.

Please make sure you're have the latest version (v1.8) of the TypeScript compiler installed in your system. Older compiler versions have proved to have issues like the one you just raised.

vulkanino commented 8 years ago

I have tsc latest version: 1.8.10. I don't understand this different behavior.

I set up a tasks.json in VS Code and now it doesn't complain anymore!