My root problem is following, I need to detect that a type is Array<any> (not any other Array type).
I can detect this with 2 combined conditions : T extends Array<any> and Array<any> extends
How could I write a conditional type without having to resort to a double ternary like following:
type foo<T> = T extends Array<any> ? Array<any> extends T ? T : 'not any array' : 'not any array';
Here is a method that works using only 1 conditional type, borrowing the trick from this answer:
There's two checks going on here: one two check for
any[], and the other two disallownever[]. To check forany[], we're using the same principle as the linked answer:anyallows us to do some crazy things, like assigning1[]to0[]. This however, also allowsnever[]to slip by.To address
never[], we use another check. Becauseneveris the bottom type, nothing is assignable to it,. This means that checking ifany[]is assignable toTis all we need.Example tests:
Playground