Can you help please. I turn from JS to TS, and immediately got into a stupor. How to write a custom object (type), which can be immediately of type Number, Boolean, Object, and Array
The basic idea: to write an abstraction, roughly speaking for a server response, the server can answer a number, an array, a Json Object, and a Boolean. It is necessary that you can handle the answer in the same way as the main types (add number, sort array, etc.), but add general methods for abstraction, such as getStatusCode (get the response code), getHeaders (), etc. d
Here's what already exists, it works, but VS code scolds, says that the original type does not have those properties that I add to it later.
type Constructor<T = {}> = new (...args: any[]) => T; interface Properties <TBase> { value: TBase } function ResponseObject<TBase extends Constructor>(Base: TBase): TBase { return class extends Base implements Properties<TBase> { public value: TBase; constructor (...values: any[]) { let thisType = typeof values[0]; if (thisType == "object" || thisType == "number" || thisType == "boolean" ) { super(values[0]); } if (Base.toString() == "Array") { super(...values); } this.value = values[0]; } public toBlaze(): string{ return "OK!"; } } } const Responsed = ResponseObject(Number); const Res = new Responsed(56.02323); console.log(Res.toBlaze()); The code itself works. But there is a mistake, and it hurts the eyes.
TBase, in the example it isNumberwhich does not really have the necessary method. - Grundy