Serginho Serginho - 23 days ago 8
TypeScript Question

Angular 2 @Input number issue

I have the following component:

<component value="3"></component>

And the component code is:

private _value:number;

get value(): number {
return this._value;

set value(value: number) {
console.log(typeof value);
this._value = value;

The log is:


But if I bind the property like:

<component [value]="variable1"></component>

In this case I get a number if variable1 is type number.


I know there's no magic with typescript but is this the right behavior? Should Angular Input decorator do the conversion?

I'm checking types in the setters, but I get errors when typescript is compiling.

I don't want to use type any in the gettes and setter.

Any elegant solution?


When binding with brackets [], the value gets bound directly on the object.

With attribute binding, the value between the quotes gets handled as string.

Maybe have a look at the docs.

working Plunker for example usage