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

is.urlString narrows the type #212

Open
risu729 opened this issue Dec 8, 2024 · 1 comment
Open

is.urlString narrows the type #212

risu729 opened this issue Dec 8, 2024 · 1 comment

Comments

@risu729
Copy link

risu729 commented Dec 8, 2024

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

@risu729
Copy link
Author

risu729 commented Dec 8, 2024

Related to #176, but I don't have any idea how to express URL in type.
I tried value is ${string}`` but it didn't work. (It seems it is treated just the same as value is string)

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

No branches or pull requests

1 participant