I'm trying add getter/setter properties to a function, TypeScript doesn't complain until I'm trying access that property.
const obj:{[key:string]:string} = {
blah: "ok",
test: "yes"
};
const func = (a:string):void => {};
for(const key in obj)
{
Object.defineProperty(func, key, {
get() { return obj[key]},
set(a:string) {
obj[key] = a;
}
});
}
console.log([func]); //good, shows function and new properties
console.log(func.blah); //error TS2339: Property 'blah' does not exist on type '(a: string) => void'.
I'm trying avoid "any" and property names as type declarations.
Any tips?
objis defined as{[key:string]:string}-- do we want to only allow accessing known propertiesfunc.blahandfunc.testor do we want to access any property? Basically you need to assert the type offuncbefore you add the properties:const func = ((a:string) => {}) as ((a:string) => void) & {[key:string]:string};