angular - Reading property names from a class in TypeScript? -


having class (e.g. wikipediasearchresult) few properties in typescript, possible access them? maybe question bit naive, i'm wondering if write following code without duplicated property names:

function mergeto(from:any, to:any={}, properties:array<string>=[]) {   if (!!from) {     (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 avoid 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 3rd party libraries such underscore.js, differs e.g. _.clone(...) since want clone specific properties , ignoring other properties may provided obj respectively from.

another way might using e.g. _.create(wikipediasearchresult.prototype, { title: 'zürich' }), haven't tried yet use javascript prototype property. great use typescript internal mechanism.

an idea came create "dummy" instance , reading key. i'm wondering if comes better variant?

you can apply each of copiable properties custom decorator:

  @copiable   lang:string; 

then inside @copiable decorator function store target property name in static collection.

function copiable(target: any, key: string)  {     copiableproperties.push(key);  } 

then later in mergeto method able iterate on copiable property names stored in copiableproperties.

it might bit overkill allow avoid duplicates. hope helps.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -