-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Array.isArray type guard not working in 5.0 #53395
Comments
The thing that changed here is that we more strictly check the |
Also run into it recently, but #17002 captures the issue here. This workaround #17002 (comment) fixes your problem |
Hmm, so the upshot is that there's a (built-in!) type predicate that can return |
There's no defect here. The above example is basically the same as declare const a: Animal | Plane;
const p = Cat.isCat(c) ? c.meow() : c.fly() But obviously If you made a new (structural) type that had all the members of I think the intuition is that |
@RyanCavanaugh Iβm not sure I follow the analogy - It still feels like a defect because effectively what we have here looks like |
According to our type system, it doesn't. Any object with all the non-mutating members of |
Indeed, so what we have in practice is an |
It's funny (like actual ha-ha funny) to me how doing the theoretically-correct thing is either clearly correct on its own merits, or total wonky weirdness, depending on who's looking at it when. I know my own view bounces around all the time on that kind of thing! |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
For what it's worth I think it's unfortunate that this behavior is "working as intended". I have a hard time worrying about theoretical non- |
This misses the point of what the purpose of type guards is. A type guard is supposed to answer the question: 'is this type safely assignable to (the other way around would of course be wrong, but that's not what is being discussed here) (also, isn't |
If there were a non- |
FWIW, an example I've just hit: I have a variable which is a JSON value type. I want to know if it's a (non-array) object. I can exclude most things with The trick is that I'm working with |
Bug Report
π Search Terms
Operator '>' cannot be applied to types
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
π Expected behavior
No error, as
Array.isArray(a)
istrue
for arraysThe text was updated successfully, but these errors were encountered: