#Rewrite to allow arbitrary deep nesting

1 messages · Page 1 of 1 (latest)

sturdy summit
#
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.
hearty niche
#

but why

stark eagle
#

numbers don't really work in TS unless you wanna get all theoretical and reconstruct them in set theory lol

sturdy summit
sturdy summit
timber oasisBOT
hearty niche
#

!ts

timber oasisBOT
#
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[][][]
hearty niche
#

yucky

stark eagle
#

sorry I meant the ability to compare numbers

hearty niche
#

but it's quite easy

hearty niche
#

extends

stark eagle
#

interesting. i'd like to see that

hearty niche
#

if you mean greater than/less than

#

it's still pretty trivial (convert to string)

stark eagle
#

is there a way to

type AGreaterThanB<NumA, NumB>
hearty niche
#

it'd just be very tedious

stark eagle
#

hmm interesting. i'll look it up. Not sure how string conversion helps here

hearty niche
stark eagle
#

oh. that doesn't sound trivial

#

i mean i guess as in easy. but

#

hard lol

hearty niche
hearty niche
stark eagle
sturdy summit
#

@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

hearty niche
hearty niche
stark eagle
#

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

sturdy summit
#

numbers stand for themselves, I guess that was needed before numbers and strings were types for themselves

hearty niche
#

(and if you passed in a von neumann ordinal you're a masochist)

sturdy summit
#

!resolved