Drew Drew - 1 month ago 10
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
is
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

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