Answers for "typescript assert non null"

0

typescript assert non null

interface Foo { bar(): void }
declare function getFoo(): Foo | undefined;

function assert(value: unknown): asserts value {
    if (value === undefined) {
        throw new Error('value must be defined');
    }
}

function test() {
    const foo = getFoo();
    // foo is Foo | undefined here
    assert(foo);
    // foo narrowed to Foo
    foo.bar();
}
Posted by: Guest on January-04-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language