I am facing a challenge in achieving the desired complex type functionality with my custom-built generic function called updateArray
:
// Updates an object array at the specified update key with the update value,
// if the specified test key matches the test value.
// Optionally pass testFailValue to set a default value if the test fails.
// Note that by passing a testFailValue ALL elements in the array will be updated at the specified update property.
// If it is omitted only elements passing the test will be updated.
export const updateArray = <T, U, V>(options: {
array: Array<T>
testKey: keyof T
testValue: U
updateKey: keyof T
updateValue: V
testFailValue?: V
}): Array<T> => {
const {
array,
testKey,
testValue,
updateKey,
updateValue,
testFailValue,
} = options
return array.map(item => {
if (item[testKey] === testValue) {
item[updateKey] = updateValue
} else if (testFailValue !== undefined) {
item[updateKey] = testFailValue
}
return item
})
}
In using TypeScript, I encountered issues with type checking in the if statement and assignment statements within the function. However, when defining types in a call signature as shown below, TypeScript provides the strict type checking I require:
interface IMyInterface {
propertyA: string
prepertyB: boolean
}
updateArray<IMyInterface, IMyInterface['propertyA'], IMyInterface['propertyB']>({
array: state.editors[editor].editorSettings,
testKey: "propertyA",
testValue: 'someValue',
updateKey: "propertyB",
updateValue: true,
testFailValue: false
})
To address the TypeScript complaints, I replaced types U
and V
with T[keyof T]
, which eliminated the errors:
export const updateArray = <T>(options: {
array: Array<T>
testKey: keyof T
testValue: T[keyof T]
updateKey: keyof T
updateValue: T[keyof T]
testFailValue?: T[keyof T]
}): Array<T> => {
const {
array,
testKey,
testValue,
updateKey,
updateValue,
testFailValue,
} = options
return array.map(item => {
if (item[testKey] === testValue) {
item[updateKey] = updateValue
} else if (testFailValue !== undefined) {
item[updateKey] = testFailValue
}
return item
})
}
However, this approach is not entirely accurate. Using T[keyof T]
allows for flexibility that may lead to assigning incorrect types to properties within the function. To ensure the correct matching of types for testValue
, updateValue
, and testFailValue
, a mechanism like typeof T[specific key]
would be ideal, where the specific key
varies based on the actual type of T
.
Is there a way to achieve this level of specificity?