Drew Drew - 1 year ago 52
TypeScript Question

What does the `is` keyword do in typescript?

I came across some code that looks like this:

export function foo(arg: string): arg is MyType {
return ...

I haven't bee able to search for
in either the docs or google, it's a pretty common word and shows up on basically every page.

What does the keyword do in that context?

Answer Source

The only use I know is the one of your example: specifying a "type predicate" (arg is MyType) in an user defined Type Guard

See User Defined Type Guards in this reference

Here is another reference