Reputation: 305
If I have an function that accept a dynamic object as a param.
const fun = <IValues>(values: IValues) => // IValues = {a: '', b: ''}
I would like to use that object and a clone that has the same keys but different values:
//manage values to init valuesModified1 and valuesModified2
const originalValues: IValues = valuesModified1; // {a: 'foo', b: 'bar'}
const modifiedValues = valuesModified2; // {a: true', b: false}
return {originalValues, modifiedValues}
my problem is that modifiedValues doesn't have the correct types.
Is there a way to clone an interface but changing all it key's values?
EDIT:
based on iY1NQ's answer, I just used : { [key in keyof IValues]: boolean }
and errors are gone (maybe is not the best solution but it's a solution):
const fun = <IValues>(values: IValues) => // IValues = {a: '', b: ''}
//manage values to init valuesModified1 and valuesModified2
const originalValues: IValues = valuesModified1; // {a: 'foo', b: 'bar'}
const modifiedValues: { [key in keyof IValues]: boolean } = valuesModified2; // {a: true', b: false}
return {originalValues, modifiedValues}
}
Upvotes: 0
Views: 2393
Reputation: 2514
That changes the type of each property to a new one specified by T
:
const fun = <IValues, T>(values: IValues): { [key in keyof IValues]: T } => {
return ...;
}
const result = fun<{ a: string }, boolean>({ a: "a"}); // => result: { a: boolean }
Upvotes: 1