Cast object to interface in TypeScript

There’s no casting in javascript, so you cannot throw if “casting fails”.Typescript supports casting but that’s only for compilation time, and you can do it like this: You can check at runtime if the value is valid and if not throw an error, i.e.: Edit As @huyz pointed out, there’s no need for the type assertion because isToDoDto is … Read more

How to apply filters to *ngFor?

Basically, you write a pipe which you can then use in the *ngFor directive. In your component: In your template, you can pass string, number or object to your pipe to use to filter on: In your pipe: Remember to register your pipe in app.module.ts; you no longer need to register the pipes in your @Component Here’s a Plunker which … Read more

How do I install Angular 2 using NPM?

at, is recommended to use QuickStart seed, here is its package.json, so actually we need to download its dependencies: you could also create your custom package.json, by running npm init, copying these dependencies (or most of them) and than running npm install with your package.json

Angular 2 Sort table columns

I assume you are loading the data (selectedData) for this template asynchronously in the component’s class, so at the start it’s not yet returned form the service, hence selectedData = undefined. There are several things you can do to mitigate that: 1. Initialize the data in the component Set the selectedData class property to an empty array so … Read more

typescript – cloning object

Solving The Specific Issue You can use a type assertion to tell the compiler that you know better: Cloning Bear in mind that sometimes it is better to write your own mapping – rather than being totally dynamic. However, there are a few “cloning” tricks you can use that give you difference effects. I will … Read more

TS cannot find modules

TypeScript relies on definition files that define the types, interfaces, etc. for libraries. I’m not familiar with FountainJS, but I’m guessing it’s smart enough to create a typings.json file in your workspace. If so, all you need to do is install typings (npm install typings –global) and then use it to install the definitions by doing typings install. If they … Read more