What does the `is` keyword do in typescript?

前端 未结 2 1469
一个人的身影
一个人的身影 2020-11-28 21:36

I came across some code that looks like this:

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

I haven\'t been able to se

2条回答
  •  庸人自扰
    2020-11-28 22:31

    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

提交回复
热议问题