我有一个在keyof
映射问题。我试图映射在Routes
类型,但是当我在映射对象则打破了Route
条件PARAMS。
type Routes = {
'/home': {}
'/pages': {
pageId: number
}
}
type IRoute<RouteName, Params> = {
route: RouteName
} & ({} extends Params ? { params?: Params } : { params: Params })
type Router = {
[RouteName in keyof Routes]: IRoute<RouteName, Routes[RouteName]>
}
type Route = Router[keyof Router]
这里params
应该要求,但TS忽略它:
const foo: Route = {
route: '/pages'
// Missing params: { pageId: number }
}
我需要qazxsw POI类型与qazxsw POI和qazxsw POI。如果qazxsw POI是一个通用的对象,然后使之选。
Route
这里是我的代码。如果你打电话routeName
它的作品如我所料。但是,当params
从映射通过params
称它打破和const foo3: IRoute<'/foo', {id: number}> = {
route: '/foo',
params: {
id: 1
}
}
const foo4: IRoute<'/foo', {}> = {
route: '/foo'
}
是可选的所有路由。
这里有一个IRoute
。
从更改条件:
IRoute
至:
keyof
见params
。