Nested Generic Types??

Heyo I have a class that takes in an object in it's constructor. I'd like the object to be a nested object with this type:
type Route = (ctx: Context) => Response<unknown>
type Router = {[key: string]: Route | Router}
type Route = (ctx: Context) => Response<unknown>
type Router = {[key: string]: Route | Router}
class MyRouter<T extends Router>{
routes: T
constructor(r: T){ this.routes = r}
}
class MyRouter<T extends Router>{
routes: T
constructor(r: T){ this.routes = r}
}
This works, but it's not generic so I don't get the autocompletion I'm after. My attempts at making this generic fail.
4 Replies
benten
bentenOP2y ago
I don't even know what to google for this tbh.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP2y ago
I want a type for an object that looks like this:
const routes = {
home: () => ({data: string}),
users: {
getUser: (id: string) => ({data: User}),
getUserList: () => {{users: User[]}},
mutate: {
addUser: (id: string) => ({data: string}),
}
}
}
const routes = {
home: () => ({data: string}),
users: {
getUser: (id: string) => ({data: User}),
getUserList: () => {{users: User[]}},
mutate: {
addUser: (id: string) => ({data: string}),
}
}
}
or something like that. The context and response types don't really matter. What I care about is actually getting the types of each entry rather than unknown
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server