Blog: Decorator

Angular: Convert input values to the correct type with a TypeScript property decorator

February 24, 2016, (updated on December 19, 2017), 10 comments, Software Development

I recently played with Angular and found an uncomfortable problem: Declaring an input property with a TypeScript type does not guarantee that the input value will always be of this type. This is because the Angular framework may update an input with a wrongly typed value and thus is able to “circumvent” the TypeScript type system. As a result, you may end up with strange and unexpected runtime errors. Let’s have a look at this simple Angular component: @Component({ selector: "my-component" }) export class MyComponent { @Input() enabled = true; showValue() { alert(typeof this.enabled); } } As shown in the […] Read more...

Tags: , , , ,

  Page 1 of 1