Having a class (e.g. WikipediaSearchResult) with a few properties in TypeScript, is it possible to access them? Maybe this question is a bit naive, but I'm wondering if I could write following code without duplicated property names:
function mergeTo(from:any, to:any={}, properties:Array<string>=[]) {
if (!!from) {
for (var property of properties) {
// no deep copy here ;-)
to[property] = from[property];
}
}
}
class WikipediaSearchResult /* implements IWikipediaSearchResult */ {
lang:string;
summary:string;
title:string;
wikipediaUrl:string;
constructor(obj?:any) {
mergeTo(obj, this, [
// --> How to avoid this list? <--
'lang', 'summary', 'title', 'wikipediaUrl'
]);
}
}
var result = new WikipediaSearchResult({
title: 'Zürich',
wikipediaUrl: 'https://en.wikipedia.org/wiki/Z%C3%BCrich'
});
console.log(result);
Of course there are some 3rd party libraries such as Underscore.js, but it differs from e.g. _.clone(...) since I do only want to clone specific properties and ignoring all other properties which may be provided by obj respectively from.
Another way might be using e.g. _.create(WikipediaSearchResult.prototype, { title: 'Zürich' }), haven't tried it yet but it will use the JavaScript prototype property. It would be great to use TypeScript internal mechanism.
An idea which I came up with is to create a "dummy" instance and reading its key. I'm wondering if someone comes up with better variant?
for (var property in from) { to[property] = from[property]; }from(any) might have more properties which should not be set into