Looking to create a generic type that can be used as an argument in a function, but struggling with defining strongly typed property names (specificProperties in the example code snippet).
type Config<T> = {
specificProperties: keyof T[],
data: T[],
}
function doSomething<T>(config: Config<T>) {
// Some logic here
}
Example of usage:
type Test = {
code: string,
name: string,
}
const config: Config<Test> = {
specificProperties: [ 'code' ], //Error occurs here
data: [
{
code: 'a',
name: 'b'
}
]
}
doSomething(config)
Error message from Typescript: Types of property 'specificProperties' are incompatible. Type 'string[]' is not assignable to type 'keyof T[]'.
Seems like Config type needs adjustment, but uncertain about how to proceed. Any suggestions?