Reputation: 4479
I made a quick playground, if you follow the link you should also see the errors, but here's the code:
const normalizeValue = (v: any): any => {
if (v === 'true') return true;
if (v === 'false') return false;
if (!Number.isNaN(+v)) return +v;
return v
};
interface defaults {
a: number,
b: boolean,
c: string,
}
const normalizeOptions = <T extends defaults>(
defaultOps: T,
inputOps: Partial<T>
): T => {
const INPUT = { ...inputOps };
const NORMAL = {};
Object.keys(defaultOps).forEach((k) => {
if (k in INPUT) {
NORMAL[k] = normalizeValue(INPUT[k]);
} else {
NORMAL[k] = defaultOps[k];
}
});
return NORMAL;
};
Why a property of Partial isn't the same type as with it's source object and how can I solve this?
Upvotes: 1
Views: 20