I need some advice on a tricky syntax problem. I have a longer list of all HTTP methods and would like to call the appropriate user function.
Simplified code:
const httpMethods = {
'connect': this.connect(req, res),
'copy': this.copy(req, res),
'delete': this.delete(req, res),
'get': this.get(req, res),
...
}
let callFunction = httpMethods['get']
callFunction(req, res)
How can I create an index signature for the object httpMethods or type cast the callFunction to avoid TS errors?
TS 7017, "Das Element weist implizit einen Typ \"any\" auf, weil der Typ \"{ 'connect': void; 'copy': void; 'delete': void; 'get': void; 'head': void; 'link': void; 'lock': void; 'options': void; 'patch': void; 'post': void; 'pri': void; 'propfind': void; 'purge': void; 'put': void; 'trace': void; 'unlink': void; 'unlock': void; 'view': void; }\" keine Indexsignatur umfasst."
Code is as follows:
let methodFunction = httpMethods[requestData.httpMethod] methodFunction(requestData, response)
– Andy Brunner Oct 29 '18 at 10:26