-
Notifications
You must be signed in to change notification settings - Fork 125
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
While checking type of something unknown, expect the correct type as input. #50
Comments
This also narrows unions. function bar(): number[] | null {
return null
}
const barValue = bar() // number[] | null
if (Array.isArray(barValue))
barValue // number[] |
I think the current implementation already covers all the cases you describe. The type annotation you propose would be functionally identical to the one we already have, because If you find a failing case with the current implementation, we can re-open. |
Re-opening, just checked #48. Taking a look at this now! |
@mattpocock Current implementation always returns EDIT: ah ok, i saw the problem |
Let's discuss this on #48 itself, because your proposed implementation doesn't fix that issue (checked locally) |
As an example, when we are doing type check on runtime with
Array.isArray(args: any)
args
isany
, first of all it can beunknown
which is nicer.But why not expect an array in the first place? So:
So when we use it, we get this results:
So if we are checking something that is already a known array, we don't make it
unknown[]
While we are also still accepting anything unknown.
Also with this, if we know something is not an
array
orunknown
orany
we get type error.Why is that important?
So let's say we had some
unknown
value in a large function and we check if it's anarray
.Then a while later we narrow the type of the value higher in the function, so now we know it's not an
array
.For example we check if it's a
string
and if it's a string, then we don't need to check if astring
is an array, because that will always return false.But now we forget the
Array.isArray()
check at the bottom, because TS didn't give any type errors.This should be same for every type check, not just
Array.isArray()
Before making a PR and etc. I wanted to here opinions on this.
And if there is a good reason that this is not being done.
The text was updated successfully, but these errors were encountered: