My goal is to set specific types based on the property name:
- If the property name starts with
on
, it should be of typeFunction
- For all other cases, the type should be
boolean
Initially, I attempted this using an interface:
interface Props {
[key: `on${string}`]: Function; // error: 'on${string}' index type 'Function' is not assignable to 'string' index type 'boolean'.ts(2413)
[key: string]: boolean;
}
After that, I tried a different approach utilizing a type and interface:
type EventName = `on${string}`;
interface Props {
[K: EventName | string]: typeof K extends EventName ? Function : boolean;
}
const props: Props = {
asd: true,
onClick: () => {}, // error: Type '() => void' is not assignable to type 'boolean'.ts(2322)
};
Is there another way to achieve this without encountering these errors?