type NestArray<T, K extends number> =
K extends 1 ? T[] :
K extends 2 ? T[][] :
K extends 3 ? T[][][] :
K extends 4 ? T[][][][] :
K extends 5 ? T[][][][][] :
K extends 6 ? T[][][][][][] :
K extends 7 ? T[][][][][][][] :
never;
type test1 = NestArray<string, 1>; // string[]
type test2 = NestArray<number, 2>; // number[][]
type test3 = NestArray<string, 3>; // string[][][] etc.
#Rewrite to allow arbitrary deep nesting
1 messages · Page 1 of 1 (latest)
but why
numbers don't really work in TS unless you wanna get all theoretical and reconstruct them in set theory lol
it works perfectly as it is, learn TS
implementing a numpy like reshape functions
they work perfectly fine
!ts
type NestArray<T, Depth extends number, Acc extends never[] = []> = Acc["length"] extends Depth ? T : NestArray<T[], Depth, [...Acc, never]>;
type Test1 = NestArray<string, 1>; // string[]
// ^? - type Test1 = string[]
type Test2 = NestArray<number, 2>; // number[][]
// ^? - type Test2 = number[][]
type Test3 = NestArray<string, 3>; // string[][][] etc.
// ^? - type Test3 = string[][][]
yucky
sorry I meant the ability to compare numbers
but it's quite easy
interesting. i'd like to see that
is there a way to
type AGreaterThanB<NumA, NumB>
it'd just be very tedious
hmm interesting. i'll look it up. Not sure how string conversion helps here
you can compare digit by digit
it's also very easy to convert to tuples and just see which one matches first... but tuples are very slow + very recursive
not hard, just requires nasty lookup tables
yeah that's the "set theory" version I meant
@hearty niche wow, you are amazing, thank you... i tested recursion, but was stuck at the accumulating lol... my test run and made TS stop evalulating lol
idk if it's just me, but i wouldn't call MyTuple["length"] set theory...
this is the normal way to iterate over numbers in TS so it's not like i did anything special
oh haha I didn't think of that. I thought you were going for a set-theoretic definition of natural numbers
Like type Zero = the null set, type One = the set containing the null set, etc
numbers stand for themselves, I guess that was needed before numbers and strings were types for themselves
you wouldn't be able to compare against the literal number type that was passed in
(and if you passed in a von neumann ordinal you're a masochist)
!resolved