0
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?

  • https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type I got the answer for this question ,thanks – 西索酱 Jan 09 '23 at 06:45

0 Answers0