safe-units icon indicating copy to clipboard operation
safe-units copied to clipboard

Can assign incompatible measures if they have some dimensions in common,

Open niklashallenfur opened this issue 4 years ago • 5 comments

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

niklashallenfur avatar Feb 29 '20 15:02 niklashallenfur

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.

jscheiny avatar Mar 25 '20 21:03 jscheiny

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

jscheiny avatar Mar 25 '20 22:03 jscheiny

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.

cloudify avatar Jul 17 '22 19:07 cloudify

Hello. I joyed that you created so library and want help solve the problem. May be this trick help you.

phpcoder2022 avatar Apr 05 '24 07:04 phpcoder2022

I deleted odd hack from my solution.

phpcoder2022 avatar Apr 05 '24 07:04 phpcoder2022