anonym
anonym

Reputation: 4850

Angular2 Error: Type 'number' is not assignable to type 'NumberConstructor'

In the following Angular2 program where I want the system to randomly choose four choices from either 0 or 1. I'm getting the desired output and the console doesn't report any error. However, my IDE displays the following error:

Angular2 Error

I tried changing Number to number but it yields another error:

Another Error

Component Code

import {Component} from 'angular2/core';
import {OnInit} from 'angular2/core';

@Component(
{
    selector: 'puzzle',
    template: `
        <section class="combination">
            I:   {{ switch1Number }}<br>
            II:  {{ switch2Number }}<br>
            III: {{ switch3Number }}<br>
            IV:  {{ switch4Number }}
        </section>
    `
})

export class PuzzleComponent implements OnInit {
    switch1Number = Number;
    switch2Number = Number;
    switch3Number = Number;
    switch4Number = Number;

    ngOnInit() {
        // Math.randon gives a random decimal value between 0 & 1.
        // Math..round rounds it to 0 or 1
        this.switch1Number = Math.round(Math.random());
        this.switch2Number = Math.round(Math.random());
        this.switch3Number = Math.round(Math.random());
        this.switch4Number = Math.round(Math.random());

        console.log(this.switch1Number, this.switch2Number, this.switch3Number, this.switch4Number);
    }
}

Upvotes: 2

Views: 15045

Answers (1)

Ankit Singh
Ankit Singh

Reputation: 24945

Replace Number with number in

switch1Number = Number

it should be

switch1Number: number;

Number is not a type of variable, it's a number constructor just likeString is a string type constructor.

Upvotes: 3

Related Questions