How can i replace all properties of B with the values of A? and omit all keys of A that are not in B.
type A = {
a: {
a: string
b: number
c: boolean
d: {
a: any
b: any
}
}
b: { b: null }
}
type B = {
a: {
a: boolean
b: boolean
d: { a: boolean }
}
}
// Result
type C = {
a: {
a: string
b: number
d: { a: any }
}
}
Edit:
Here is an example why @catgirlkelly answer doesn't work in my case.
type DeepReplace<T, U> = T extends object ? U extends object ?
{ [K in keyof T]: K extends keyof U ? DeepReplace<T[K], U[K]> : T[K] } : U : U
type Override<Source, Target> = Source extends object ? {
[K in keyof Source]: K extends keyof Target ? Override<Source[K], Target[K]> : Source[K];
} : Target;
type QueryObj<T> = T extends object ? {
[Key in keyof T]?: QueryObj<T[Key]>;
} : NonNullable<T> extends object ? never : boolean;
type GRAPH_QL_TYPE = {
id: string,
name: string,
Assets?: {
items: ({
id: string,
name: string,
} )[],
} ,
};
function query<Query extends QueryObj<GRAPH_QL_TYPE>>(query: Query) {
// Doesn't work
return {} as Override<Query, GRAPH_QL_TYPE>
// Works
// return {} as DeepReplace<Query, GRAPH_QL_TYPE>
}
const res = query({
name: true,
id: true,
Assets: {
items: [
{
name: true,
id: true,
}
]
}
})
// res.Assets.items[0].name = true with Override, and it equals string with DeepReplace
const reactState: GRAPH_QL_TYPE = res