javascript - Get field type in Typescript -


i have class in typescript. has fields

field1 : ko.observable<number>(); field2 : ko.observable<string>(''); ... 

in function in class i've tried loop through every object field , fields of type number need perform modifications.

how can detect this?


i've tried next, type string logically. locationmodel filled values html form, inputs of type text

 object.keys(locationmodel)  .foreach(property => {                 if (typeof locationmodel[property]() === 'number') { }                 else{ }             }         }); 

if want put automatic type conversion knockout can create extender that.

in basic form return writable computed either:

  • converts incoming value on write , stores converted value in observable, or
  • stores incoming value in observable unchanged , converts on read.

an implementation of latter – run snippet below , note how type reflects in viewmodel:

ko.extenders.type = function (target, type) {      var typedvalue = ko.purecomputed({          read: function () {              var value = target();              if (typeof value === "undefined" || value === null) {                  return value;              } else {                  return type(value);              }          },          write: target      });      typedvalue.raw = target;      return typedvalue;  };    var vm = {      num: ko.observable().extend({type: number}),      str: ko.observable().extend({type: string})  };    ko.applybindings(vm);    // init values  vm.num(100);  vm.str(100);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>    num: <input data-bind="value: num"> (raw: <span data-bind="text: num.raw"></span>)<br>  str: <input data-bind="value: str"> (raw: <span data-bind="text: str.raw"></span>)<br><br>    <hr>  view model:<br>  <pre data-bind="text: ko.tojson($root, null, 2)"></pre>


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -