1

I have some variables which are all numbers in Typescript and I want to make some calculations on them. I would like if possible to do something like this:

example:

type Newton = number;
type Mass = number;
type Acceleration = number;

type Second = number;
type Distance = number;
type Speed = number;


// Newton <- Mass * Acceleration
// Speed <- Distance / Second

so if I try to do this it will succeed:

const mass: Mass = 3;
const acceleration: Acceleration = 2;
const force: Newton = mass * acceleration // succeeds

but here should fail:

const distance: Distance = 3;
const time: Second = 2;
const speed: Speed = distance * time // Error: cannot bind type 'Distance x Second' to type 'Speed'

Is this somehow possible ?

but-why
  • 533
  • 3
  • 10

0 Answers0