0

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

14
  • "I have to use "new" keyword for utilizing this function" - no. What made you think that? Commented May 10, 2020 at 16:04
  • @Bergi That is the use of it in this case. I haven't created this function which my example above is just a shortened mock of it, and I need to declare a typescript module for it. Commented May 10, 2020 at 18:26
  • I don't think there's enough information here to advise you on your first question: what is the relationship between the passed-in config parameter to the constructor and the object that comes out? The example code doesn't seem to be complete enough to give an idea what property names are and are not accepted. Commented May 10, 2020 at 19:47
  • 1
    If all you know about the class instances is that it will have a string value for every possible key then you are probably doing the best you can do here. Maybe string | undefined instead of string if you want users to be more careful? So I'd say this is "on the right path" given the info I have. Not sure that warrants an answer though, or even a programming SO question as opposed to a question on Code Review Stack Exchange... Commented May 11, 2020 at 14:45
  • 1
    @Ege Is that not how you declare the type of a variable (without creating the variable itself) in TypeScript? Edit: Ah, I guess I forgot the colon. declare var createProxy: { new(config: …): … }; Commented May 11, 2020 at 18:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.