Saturday 15 May 2010

Is there a way to create nominal types in TypeScript that extend primitive types? -



Is there a way to create nominal types in TypeScript that extend primitive types? -

say have 2 types of numbers i'm tracking latitude , longitude. represent these variables basic number primitive, disallow assignment of longitude latitude variable in typescript.

is there way sub-class number primitive typescript detects assignment illegal? someway coerce nominal typing code fails?

var longitude : longitudenumber = new longitudenumber(); var latitude : latitudenumber; latitude = longitude; // <-- type failure

the reply "how extend primitive type in typescript?" seems set me in right direction, not sure how extend solution create distinct nominal sub-types different kinds of numbers.

do have wrapper primitive? if so, can create behave seamlessly normal number or have reference sub-member? can somehow create typescript compile-time number subclass?

there isn't way this.

a suggestion tracking on github site units of measure.

in future release, you'll able utilize type define alternate names primitives, these not have checking associated them:

type lat = number; type lon = number; var x: lat = 43; var y: lon = 48; y = 'hello'; // error x = y; // no error

typescript

No comments:

Post a Comment