How can I provide type annotations to inform TypeScript that this code is correct?
Indeed, I do require that inline function class. The code presented here is a simplified version of my actual problem.
let x = 10;
const obj = new (function() {
if(--x) return this.constructor();
return {};
});
'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009)
I attempted fully annotating everything but it did not resolve the issue. Here is what it looks like with annotations:
type myType = { constructor(): myType };
let x: number = 10;
const obj: myType = new (function(): myType {
if(--x) return (this as myType).constructor();
return {} as myType;
});
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009)
Is there something in the TypeScript documentation that I might be overlooking? It appears that there are no generic Constructor types and the available Constructor types are not specifically for constructors.