With TypeScript 1.6 is it possible to write functions to check objects against interfaces. (Below is the anncouncement from MSDN and how to use it.)
My question targets the return expression return a.name === 'kitty';
:
- Plays it an role?
- Must all cats called kitty?
User defined type guards
[http://blogs.msdn.com/b/typescript/archive/2015/09/16/announcing-typescript-1-6.aspx]
In earlier versions of TypeScript, you could use if statements to narrow the type. For example, you could use:
if (typeof x === "number") { … }
This helped type information flow into common ways of working with types at runtime (inspired by some of the other projects doing typechecking of JS). While this approach is powerful, we wanted to push it a bit further. In 1.6, you can now create your own type guard functions:
interface Animal {name: string; } interface Cat extends Animal { meow(); } function isCat(a: Animal): a is Cat { return a.name === 'kitty'; } var x: Animal; if(isCat(x)) { x.meow(); // OK, x is Cat in this block }
This allows you to work with not only typeof and instanceof checks, which need a type that JavaScript understands, but now you can work with interfaces and do custom analysis. Guard functions are denoted by their “a is X” return type, which returns boolean and signals to the compiler if what the expected type now is.