so originally to enforce a constraint, i\'d just apply an interface like this
// this is the constraint
interface Topic {
[key: string]: (...args: any[]) =>
One option is to use a no-op function that has the relevant generics in the argument so it constrains in place:
(playground)
// this is the constraint
interface Topic {
[key: string]: (...args: any[]) => Promise
}
function ensureTopic(topic: T){
return topic;
}
// object that must pass the constraint
const topic = ensureTopic({
// GOOD: topic methods must conform
async actuate(a: boolean) {}
})
related: Use function interface to ensure parameters but infer more specific return type