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
Set.has typing #42641
Comments
Checking the existence of a value that can't be present (according to the type system) should be an error. Why would you want it to be different? In your local code you could just use |
I've came with this issue because now users need to add unnecessary type guards to make a safe operation in their code. An example: const set: Set<number> = new Set([])
const list: unknown[] = [1, '2', {}]
list.map((item) => {
if (typeof item === 'number') { // you don't need this type guard, but the code won't compile without that
return set.has(item)
}
return false
}) |
Right, well as Martin says, either do:
Or:
...if you want do something like your example code without adding any code that will be present at run time. Although if it were me I'd generally write the version with the type guard! |
Ideally this would be a bivariant parameter; see #14520. The current behavior is the best available compromise. You're free to add the |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
lib Update Request
Missing / Incorrect Definition
The
Set.has
method's type expects thatvalue
type istypeof T
:https://github.com/microsoft/TypeScript/blob/master/src/lib/es2015.collection.d.ts#L43
And this definition makes safe-code uncompilable.
Sample Code
sandbox
Documentation Link
https://tc39.es/ecma262/#sec-set.prototype.has
Well what do you think about replacing this method definition with this:
The text was updated successfully, but these errors were encountered: