safe-units
safe-units copied to clipboard
Can assign incompatible measures if they have some dimensions in common,
The following, taken from the documentation
const error: Velocity = length.times(time);
// ERROR: A measure of m*s isn't assignable to a measure of m/s.
Gives a compile-time error.
But removing the times
operation, making it just
const error: Velocity = length
Does not give a compile-time error, although the measures are clearly incompatible
Am I missing something? I have strict
turned on in my tsconfig file.
Thanks otherwise for a fantastic library
Hey, thanks for bringing this to my attention! This actually exposes a pretty fundamental issue with the library that's gonna be pretty difficult to fix since typescript is structurally typed. I definitely should've thought of this sooner. This is going to take a while to resolve but my intuition right now is that we will need to make more stringent restrictions on custom dimensions that users can create in the library.
Actually this might be doable now. I had assumed that exact types would require a language update but it seems they are possible now! Will work on integrating this clever bit of type hackery into the code soon:
https://github.com/microsoft/TypeScript/issues/12936#issuecomment-524631270
hello @jscheiny , thanks for this wonderful library - I think I just found another case where the library allows mixing units:
const a = length.times(time);
const b = length.plus(a); // <- this should not be allowed
I'm happy to help.
Hello. I joyed that you created so library and want help solve the problem. May be this trick help you.
I deleted odd hack from my solution.