class MyNumber {
private __value: number;
static from(value: any): MyNumber | null {
if (typeof value === 'number') {
return new MyNumber(value);
}
return null;
}
constructor(value: number) {
this.__value = value;
}
to(): any {
return { type: 'NUMBER', value: this.__value };
}
}
class MyString {
private __value: string;
static from(value: any): MyString | null {
if (typeof value === 'string') {
return new MyString(value);
}
return null;
}
constructor(value: string) {
this.__value = value;
}
to(): any {
return { type: 'STRING', value: this.__value };
}
}
class MyBoolean {
private __value: boolean;
static from(value: any): MyBoolean | null {
if (typeof value === 'boolean') {
return new MyBoolean(value);
}
return null;
}
constructor(value: boolean) {
this.__value = value;
}
to(): any {
return { type: 'BOOLEAN', value: this.__value };
}
}
I want to define a type so that only classes of a specific form are accepted as arguments.
Please tell me what type should for Klass be defined in the code below.
function func(Klass, value: any): any {
const instance = Klass.from(value);
return instance ? instance.to() : null;
}
const output = hello(MyNumber, 3);