#Nested string literal constrainers

10 messages · Page 1 of 1 (latest)

umbral trellisBOT
#
scinorandex#9710

Preview:```ts
type OuterT<
T extends string,
InnerThing extends string

= {
[key in T]: InnerT<InnerThing, key>
}

type InnerT<I extends string, Context> = {
[key in I]: (e: Context, innerKey: key) => void
}

function outer<O extends string, I extends string>(
p: OuterT<O, I>
)
...```

native gale
#

I don't know how to flatten something like this down to an object type

#

I can't use a plain object since keys can only affect the types of their values when they're "linked" to a function generic

umbral trellisBOT
#
n_n#2622

Preview:```ts
type OuterT<T extends string> = {
[Key in T]: InnerT<any, Key>
}

type InnerT<I extends string, Context> = {
[Key in I]: (e: Context, innerKey: Key) => void
}

function outer<
O extends OuterT<O_>,
O_ extends keyof O & string = keyof O & string

(p: O) {
return p
}
...```

rotund plinth
#

here's one way

#

or you can avoid inner completely!

umbral trellisBOT
#
n_n#2622

Preview:```ts
type Outer<O> = {
[Key in keyof O]: Inner<O[Key], Key>
}

type Inner<O, Context> = {
[Key in keyof O]: (e: Context, innerKey: Key) => void
}

function outer<O extends Outer<O>>(p: O) {
return p
}
...```

rotund plinth
#

@native gale ^

native gale
#

Thanks

#

I've tested that it works and kinda get where the source of truth is