Ts-morph: [Req] Add Typescript Type Guards documentation

Created on 5 Oct 2019  路  3Comments  路  Source: dsherret/ts-morph

Do you support User Defined Type Guards?

May I ask you to add documentation about this?

/**
 * Just some interfaces
 */
interface Foo {
    foo: number;
    common: string;
}

interface Bar {
    bar: number;
    common: string;
}

/**
 * User Defined Type Guard!
 */
function isFoo(arg: any): arg is Foo {
    return arg.foo !== undefined;
}

How to get arg is Foo? What is the relation to this block and arg as a parameter? How to get type of Foo.

It would be great if add this to your documentation.

question documentation

All 3 comments

See here:

https://ts-ast-viewer.com/#code/GYVwdgxgLglg9mABDAzgMTnAFAQwE4DmAXIjmAJ4CUJ+ByKiGciA3gLABQi3ieAplBB4ktAHTBMiAIQBeGYnAATPsBhg+igNycAvkA

It's a TypePredicate. I just wrapped that node in 4.2.0 (see #722).

I don't know if I'm going to add documentation for it though beyond what's in the JS docs (I need to revamp the documentation).

@dsherret

I can not find any getTypePredicate() or something like this.

image

May I ask you to share an example?

Use getReturnTypeNode() then check if that is a type predicate using TypeGuards.isTypePredicateNode.

Was this page helpful?
0 / 5 - 0 ratings