-
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
Array.isArray problem with narrowed type #48
Comments
@mattpocock So
|
@DeepDoge Your meaning is unclear from the comment above. I don't see anything wrong with OP's issue. |
@mattpocock well then this fixes OP's issue: isArray<T>(arg: T | T[]): arg is T[] extends T ? T[] : any[]; it takes advantage of these returning type _ = any[] extends any ? true : false
type _ = unknown[] extends unknown ? true : false while these returns false type _ = number[] extends number ? true : false
type _ = string[] extends string ? true : false
type _ = {}[] extends {} ? true : false It passes all test. |
@DeepDoge Amazing work! Truly, thanks for your patience and skill in figuring this out. Feel free to PR (I'd love your name on the contributors list) and we can ship this. |
I'm confused, if |
@guillaumebrunerie if input is Basically, all this is doing is if the input is if you have a function like: foo<T>(foo: T | T[]): T when you give it |
So can the PR #56 be merged now? |
Hi, I recently saw your YT video and wanted to add ts-reset to our project. But there is an issue which I find difficult to overcome:
Basically the problem is that isArray is always adding the type
unknown[]
to my variable, which I know is of typeT | T[]
.So when I check whether it's an array, I should know it's
T[]
, but TS now thinks it'sT[] & unknown[]
which is a problem.I tried to replicate it here:
https://www.typescriptlang.org/play?ts=4.9.5#code/JYOwLgpgTgZghgYwgAgIJSnAngYQPYgDOYUArgmHlMgN4BQyywh6mWAFHFAOYBcycEFgCU-LtyaFkpEAGsQeAO4gA2gF0A3HQC+dOmCwAHFAFUQXNgB4AKgD5kAXmTXkEAB6QQAEymtsl0BhoZBN7AH4Q5H5rLToYGQpgAmRIYhtbdgA3OAAbUgho4Vo6AEhShAJiJhAQYKdLAA1XDwhvKTMLfzsMzP4G9SKHe3oSkoB6MeRS3TKS4Bhkdj8sADpmZazc-OEikbma6E28iGEtEt1tIA
Any advice maybe? :)
The text was updated successfully, but these errors were encountered: