I'm currently working with Angular and TypeScript and I want to know if it's possible to bind component @Inputs to use specific types?
Example
@Component({selector: 'foobar'})
export class FooComponent {
@Input() foo: number;
@Input() bar: boolean;
}
<foobar foo="123" bar="true"></foobar>
When the component is bound, both foo and bar are string. Does angular provide a way to enforce the specified type?
I tried this, but I don't like it...(it seems dirty and not very elegant)
@Component({selector: 'foobar'})
export class FooComponent {
foo: number;
bar: boolean;
@Input('foo') set _foo(value: string) {
this.foo = Number(value);
}
@Input('bar') set _bar(value: string) {
this.bar = value === 'true';
}
}
It would be nice if there was something in Angular's Input that could act as a binding delegate; For example:
@Input('foo', (value: string) => Number(value)) foo: number = 123;