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

isFinite(), isInteger(), ... as type predicates? #58916

Closed
6 tasks done
titouandk opened this issue Jun 18, 2024 · 3 comments
Closed
6 tasks done

isFinite(), isInteger(), ... as type predicates? #58916

titouandk opened this issue Jun 18, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@titouandk
Copy link

titouandk commented Jun 18, 2024

πŸ” Search Terms

Use isNumber() isNan() isFinite() as type predicates

βœ… Viability Checklist

⭐ Suggestion

Number provides several functions that could be used as type predicates.

Currently, those functions provide no indication about the type of the value that passes the test:

let val: unknown;

if (Number.isInteger(val)) {
    val.toString() // error: val is of type unknown
}

πŸ“ƒ Motivating Example

The exemple is self-explanatory.

πŸ’» Use Cases

What workarounds are you using in the meantime?

Manual casting to a related type.

@whzx5byb
Copy link

Duplicate of #15048

@fatcerberus
Copy link

To be clear, these can't be type predicates today because x is number is an "if and only if" relation - if the predicate is false, TS assumes x is not a number.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 18, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants