I have something like this:
function createProxy ( configObj ) {
//...some code
return new Proxy({}, {
get: ( target, name ) => {
// ...does things
return Reflect.get(target, name);
},
set: function(target, name, value){
// ...does things
return Reflect.set(target, name, value);
},
}
}
I have to use "new" keyword for utilizing this function and this is how I use it:
const proxy = new createProxy({ someConfig: string })
proxy.foo = 'bar'
And I have declared this module like this:
declare module 'create-proxy' {
declare class createProxy {
constructor(config: { someConfig?: string })
[ index: string ]: string
}
export { createProxy }
}
And I have no type errors. But...
I have two concerns about this (I am new to Typescript module declaration, my typing can be totally wrong, and that is exactly what I need help about. I have read all the related Typescript docs but couldn't solve it on my own. I just need an experienced person to show me the way),
1- [index: string]: string
seems too generic to me and my initial thought was "I think I should define the types of get
and set
methods of proxy somewhere in my declaration to be able to shape the type of indexable properties. I just feel it needs something more but I am not really experienced declaring modules and I can't point my finger at the problem.
2- What this module exports is actually a function that I need to call with the new
keyword but what I am declaring is a class. That is because I have tried declaring it as a function but then I would get the type error saying that this function does not have a constructor (Because I am calling it with the new
keyword when I am utilizing the function). I feel this is off, too.
Could you give me a hand here, please? Am I too confused or on the right path?
typescript 3.8.3