Open JoshuaKGoldberg opened 1 month ago
There is no heuristic that fits all use cases I think. For example, I might argue that Price * TaxRate
should be allowed.
To serve this feature request, I think TypeScript needs to support the βtrueβ nominal typing so that interaction between different types could be made configurable.
Even with nominal typing you'd need something like #42218 for it to have any effect on mathematical operators.
There is no heuristic that fits all use cases I think. For example, I might argue that
Price * TaxRate
should be allowed.To serve this feature request, I think TypeScript needs to support the βtrueβ nominal typing so that interaction between different types could be made configurable.
The matching types are only really required for addition or subtraction.
For example, 5m/s + 3m/s makes sense, but 5m/s + 2 hours has no useful outcome. Multiplication and division will generally create new units: 1$ / 2β¬ = 0.5 $/β¬
π Search Terms
number numeric branded types plus minus add subtract
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAwgrgJwRAdgYxAHgCoD4oC8UKcAtgEYQJQBkUA3lAPpNqLLogBcU2UAvgG4AUKEhQAoogD2hWO1QZMAIggzluEWOgBVAMoAROfCSKsyuAGcAJhpHCAZnHTAAltJRQ0AQxQAFRDQAC29LCABJYAhSAAowBFc0CJQpBGkeVOkAGigAd28AGwKIYHCUfQMeCoBKBmEoBqgAeiaoADlZbSgqNIQc8jhgKHC86TgC6ygQgDdoCAAPSDQoyY8IesbkYERPfKKSsoqofHjE5MyRfmEgA
π» Code
π Actual behavior
Euro
andUSD
-two different branded numeric types- can be compared with binary operators such as-
.π Expected behavior
These are two different branded numeric types. I would have expected that binary operations between the two of them would be disallowed.
More technically, if a mathematical binary operation contains:
number
and some object type...then I'd expect to receive a type error.
Additional information about the issue
Per #202, TypeScript doesn't actually formally include a built-in branded/nominal typing. But other mismatched operators are disallowed [playground of mismatched operators]: