1

any seems to both extend and not extend any[] i.e. it returns both values 1 | 2:

type Test<T> = T extends any[] ? 1 : 2;
type Result = Test<any>; // 1 | 2

Is this correct? And how could I determine the difference between being passed an any vs an Array<any>?

I am trying to use it in the following example and it's causing unexpected results (not sure if the problems are related):

type Push<T, A = []> = [T, ...(A extends any[] ? A : [A])];
type PushTest = Push<5, any>; // [5, ...any[]]

I would expect PushTest to be [5, any] not [5, ...any[]]. Push<5, 6> works (returning [5, 6]) and using unknown also works, but I haven't been able to get any to work.

This is just part of my experimentation to gain a deeper understanding of TS.

Thank you for any help.

Eli
  • 85
  • 3
  • Please see the answer to the linked question for information. – jcalz Aug 02 '22 at 02:16
  • Yes, this is a duplicate of the linked task where the response was: "any matches anything, so return the union of both branches". But I would still like to know how to differentiate between any and any[]. – Eli Aug 02 '22 at 03:03
  • 1
    [This one](https://stackoverflow.com/questions/55541275/typescript-check-for-the-any-type) shows how to distinguish `any` from other types. You can also distinguish because, for example, `unknown extends any[]` is *false*, while `unknown extends any` is *true* – jcalz Aug 02 '22 at 03:06

0 Answers0