I am attempting to map several Typescript types into one combined type via Typescript generic tuples. Example I would like to see:
// object 1
const foo = {
user: {
id: 1,
},
};
// object 2
const bar = {
hello: 'goodbye',
};
// later, after some code that combines the above objects into one
const baz = {
user: {
id: 1,
},
hello: 'goodbye',
};
I've gotten somewhere with generic tuples, and though the Typescript language server correctly autocompletes when I try creating an object, it errors when I try to use the mapped properties afterwards:
const foo = {
user: {
id: 1,
},
};
const bar = {
hello: "goodbye",
};
type Test<T extends Record<PropertyKey, any>[]> = (
T[number] extends any ? (k: T[number]) => void : never
) extends (k: infer I) => void
? I
: never;
const test: Test<[typeof foo, typeof bar]> = {
user: {
id: 1
},
hello: "23",
};
console.log(test.user);
^
/* Property 'user' does not exist on type '{ user: { id: number; }; } | { hello: string; }'.
Property 'user' does not exist on type '{ hello: string; }'.ts(2339) */
I am declaring a type that is mapped of all the generic tuples passed to it, it correctly autocompletes when I am creating a new object from the given types, but when I attempt to access those properties it errors!
Any reason this doesn't work, or am I not correctly typing this properly?