This change brings increased type safety to uses of XmlAttributeComponent. Now the compiler is checkign for us that the properties that get passed in to every subclass match the intended interface, and also that the xmlKeys property -> xml attribute mapping has all the right keys
26 lines
720 B
TypeScript
26 lines
720 B
TypeScript
import { BaseXmlComponent } from "./base";
|
|
|
|
type AttributeMap<T> = {[P in keyof T]: string};
|
|
|
|
export abstract class XmlAttributeComponent<T> extends BaseXmlComponent {
|
|
protected root: T;
|
|
protected xmlKeys: AttributeMap<T>;
|
|
|
|
constructor(properties: T) {
|
|
super("_attr");
|
|
this.root = properties;
|
|
}
|
|
|
|
public prepForXml(): {_attr: {[key: string]: (string | number | boolean)}} {
|
|
const attrs = {};
|
|
Object.keys(this.root).forEach((key) => {
|
|
const value = this.root[key];
|
|
if (value !== undefined) {
|
|
const newKey = this.xmlKeys[key];
|
|
attrs[newKey] = value;
|
|
}
|
|
});
|
|
return {_attr: attrs};
|
|
}
|
|
}
|