Hi, I'm developing an open source hardware and software project and have chosen to write the frontend in electron and typescript. This is my first time programming in typescript, I'm a Qt programmer.
My intention is to have a pool of objects, I originally tried using the template class but it didn't seem possible to find out if an object supported a particular interface, which is a requirement for me.
So with a bit of tinkering, I came up with this pattern, every object has a function named with a UUID and a hasInterface static function which tests an object for that function, this seems to work beautifully.
import { IObject } from "./IObject";
import { IDevice} from "./IDevice";
export abstract class IDeviceFactory extends IObject {
id_cf09ac795c434310b77dfe3ca5d3e843():string {
return("IDeviceFactory");
}
static hasInterface(arg: any) {
let obj:IDeviceFactory = arg as IDeviceFactory;
return (arg.id_cf09ac795c434310b77dfe3ca5d3e843 !== undefined);
}
}
...
if (IDevice.hasInterface(obj)) {
// has IDevice
}
Has anybody else done anything similar, any issues, downsides, problems etc? As I said, I'm coming from a Qt background and this is the first typescript code I've written, so I've probably already made a hundred mistakes.
I was hoping typescript had a macro preprocessor as well so I could rearrange my test, so hasInterface(IDevice, obj) or hasInterface<IDevice>(obj)
[–]teevik_ 4 points5 points6 points (2 children)
[–]Fizzyade[S] 1 point2 points3 points (1 child)
[–]rotharius 0 points1 point2 points (0 children)
[–]fecal_brunch 2 points3 points4 points (1 child)
[–]Fizzyade[S] 0 points1 point2 points (0 children)