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

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

What return type should be used for setTimeout in TypeScript?

Simplest solution is to allow type inference to work and not specify any type at all. If you need to specify a type, seeing as the type is not consistent between the browser and node declarations, you could use ReturnType to specify that the type of the variable is whatever the return type of setTimeout is: Alternately, window.setTimeout can also be … Read more

‘tsc command not found’ in compiling typescript

A few tips in order restart the terminal restart the machine reinstall nodejs + then run npm install typescript -g If it still doesn’t work run npm config get prefix to see where npm install -g is putting files (append bin to the output) and make sure that they are in the path (the node js setup does this. Maybe … Read more

cannot redeclare block scoped variable (typescript)

The solution for me was to convert my code from using CommonJS (require, module.exports, etc) to ES Modules (import, export, etc.) The TypeScript documentation also appears to recommend ES Module syntax: TypeScript Modules Of course there’s a lot more to it than this, but as a start: Replace instances of require with import, e.g. Replace: … Read more