Skip to content

is.urlString narrows the type #212

Open
@risu729

Description

@risu729

urlString narrows the type to string, which is correct, but it also narrows the type to non-string types in the else clause.
It is wrong type assertion as the value can be non-url strings.

Notice that TypeScript not only knows that pet is a Fish in the if branch; it also knows that in the else branch, you don’t have a Fish, so you must have a Bird.
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

is/source/index.ts

Lines 755 to 766 in e097645

export function isUrlString(value: unknown): value is string {
if (!isString(value)) {
return false;
}
try {
new URL(value); // eslint-disable-line no-new
return true;
} catch {
return false;
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions