I have some problems with Typescript and "@botstan/Magic" library in nodejs.
before we get started please read "Magic" documentation.
follow lines:
import Magic from "@botstan/magic";
import * as _ from "lodash";
@Magic
export default class Base {
[key: string]: any;
props = {};
constructor (config = {}) {
const self = this;
_.forEach(config, (value, key) => {
self.__set(key, value);
});
}
__isset (name: string): boolean {
name = _.snakeCase(name);
return _.has(this.props, name);
}
__unset (name: string): void {
name = _.snakeCase(name);
_.unset(this.props, name);
}
__set (name: string, value: any): void {
name = _.snakeCase(name);
if (value !== null || value !== undefined) {
_.set(this.props, name, value);
return;
}
_.unset(this.props, name);
}
__get (name: string): any {
name = _.snakeCase(name);
return _.get(this.props, name, undefined);
}
}
If you already seen "Magic" documentation, you know we just created a Magic class in Typescript.
import Base from "./Base";
const obj = new Base();
obj.first_name = "Jone";
obj.last_name = "Done";
console.log(obj.first_name); // Jone
console.log(obj); // { props: { first_name: "Jone", last_name: "Done" } }
The "Base" class works well, but our problem started when i got extends from it.
import Base from "./Base";
export default class Example extends Base {
someVar: any = false;
someMethod () {
}
}
For now, let me test Example class.
import Example from "./Example";
const obj = new Example();
obj.first_name = "Jone";
obj.last_name = "Done";
console.log(obj.first_name); // Jone
console.log(obj); // { props: { }, first_name: "Jone", last_name: "Done" }
The "Example" class doesn't works like The "Base" class. What i can do to fix that ?