#Check if Array is type T[] or [<some information>]

30 messages · Page 1 of 1 (latest)

white juncoBOT
#
nop#2745

Preview:```ts
type a1 = unknown[]
type a2 = number[]
type t1 = [number, ...unknown[]]
type t2 = [number, string, ...unknown[]]
type t3 = [number, string, boolean, ...unknown[]]

// Both have length number, but how would I differentiate?```

quartz swallow
#

@terse abyss tuples extend [infer First, ...infer Rest], arrays shouldnt

#

well

#

tuples that dont start with a spread element

#

you could also try T[number] extends T[0]

terse abyss
#

and the elements following it can be not unknown

quartz swallow
terse abyss
#

oh

quartz swallow
#

!:isequal

#

dang

#

not a thing

terse abyss
#

yeah that might work

#

what do you use for isEqual

#

i use the one from tsafe

quartz swallow
#

search history here

terse abyss
#

type IsMutuallyAssignable<T, S> = [T, S] extends [S, T] ? true : false;
type IsEqual<X, Y> = (<T>() => T extends X ? true : false) extends (<T>() => T extends Y ? true : false) ? true : false

class A { private a?: string }
class B { private a?: string }

type X = IsEqual<A, B>
// ^? - type X = false
type Y = IsMutuallyAssignable<A, B>
// ^? - type Y = false

#

is it this one

#

or

#

!:is-equal

white juncoBOT
#
T6#2591
`!t6:is-equal`:
type IsEqual<X, Y> = (<T>() => T extends X ? true : false) extends (<T>() => T extends Y ? true : false) ? true : false
quartz swallow
#

yea

#

ah, has a dash

terse abyss
#

I kinda help improve the docs experience

#

is TypeScript bot os

#

ight, it works good enough

#
type isTuple<T extends unknown[]> = IsEqual<T[0][], T>;
#

!close

#

thanks @quartz swallow