Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some type guards under assert are not working #186

Closed
wdzeng opened this issue Jul 16, 2023 · 1 comment · Fixed by #187
Closed

Some type guards under assert are not working #186

wdzeng opened this issue Jul 16, 2023 · 1 comment · Fixed by #187

Comments

@wdzeng
Copy link
Contributor

wdzeng commented Jul 16, 2023

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
}
@wdzeng wdzeng changed the title type guard for assert is not working type guards for assert are not working Jul 16, 2023
@wdzeng wdzeng changed the title type guards for assert are not working Some type guards under assert are not working Jul 16, 2023
@wdzeng
Copy link
Contributor Author

wdzeng commented Jul 16, 2023

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!
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant