type DataStore = {
[K in pageName]: {
page: K
data: A[K] extends { roleId: string }[] ? A[K] : never
}
}[keyof A]
interface A extends Record<pageName, unknown> {
[pageName.q]: DataA[]
[pageName.w]: DataB[]
}
enum pageName {
q = '1',
w = '2',
e = '3',
//...
}
interface DataA { roleId: string, name:string }
interface DataB { roleId: string, age: string }
let info: Record<number, DataA> | Record<number, DataB>; // 请求发过来的数据
(Object.entries(info) as unknown as [string, DataStore['data'][number]][])
.map(([page, data]) => {
// here the Type derivation of data is DataA | DataB
// but in my hope it should be DataA & DataB
})
I plan to handle it uniformly in map, but the current derived type allows me to only get the value data.roleId
, and the other values are checked by the type. How can I modify it so that I can use data.
to bring out other properties?