I am using typescript@2.3.4.
I have a target object:
interface MyTarget {
a: string;
b: string;
c: string;
d: string;
}
I want to create multiple converts of partial objects using generics.
One such convert might look like this:
const convert = <T extends object>(t: T): MyTarget => {
return {
c: "c",
d: "d",
...t,
};
};
Yet this yields into:
error TS2698: Spread types may only be created from object types.
even though I guard the generic T
to be an object
.
I then remember that there is a Partial
type, hence I tried this:
const convert = (partial: Partial<MyTarget>): MyTarget => {
return {
c: "c",
d: "d",
...partial,
};
};
Yet the Partial
makes all properties optional. I don't want that and it would now throw:
src/Partial.ts(14,5): error TS2322: Type '{ a?: string; b?: string; c: string; d: string; }' is not assignable to type 'MyTarget'.
Property 'a' is optional in type '{ a?: string; b?: string; c: string; d: string; }' but required in type 'MyTarget'.
I want to create an instance of MyTarget
with every field set as an requirement. I do want to keep typesafety, which is why I don't want to this even though it works:
const convert = (partial: Partial<MyTarget>): MyTarget => {
return {
c: "c",
d: "d",
...partial,
} as MyTarget; // loses type checks, really don't want to
};