sindresorhus / is

Type check values

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some type guards under assert are not working

wdzeng opened this issue · comments

Several type guards under assert are not working.

import { assert } from '@sindresorhus/is'

function foo(s: string | undefined) {
    assert.truthy(s)
    // Type 'string | undefined' is not assignable to type 'string'.
    // Type 'undefined' is not assignable to type 'string'. ts(2322)
    const bar: string = s
}

Nonetheless type guard for is is working well.

import is from '@sindresorhus/is'

function foo(s: string | undefined) {
    if (!is.truthy(s)) {
        throw TypeError(`s is not a string: ${s}`)
    }
    const bar: string = s  // Good!
}

This can be verified by looking at dist/index.d.ts (line numbers in npm webpage do not align with the code well). For example:

// dist/index.d.ts (v5.5.0)
declare namespace is {
    var truthy: <T>(value: Falsy | T) => value is T;     // L66
    var falsy: <T>(value: Falsy | T) => value is Falsy;  // L67
}
type Assert = {
    truthy: (value: unknown) => asserts value is unknown;   // L204
    falsy: (value: unknown) => asserts value is unknown;    // L205
}

Hi, I created a PR for it. This should fix the problem.

import { assert } from '@sindresorhus/is'

function foo(s: string | undefined) {
    assert.truthy(s)
    const bar: string = s  // Good!
}