Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow math operations on different branded numeric types #59423

Open
JoshuaKGoldberg opened this issue Jul 25, 2024 · 3 comments
Open

Don't allow math operations on different branded numeric types #59423

JoshuaKGoldberg opened this issue Jul 25, 2024 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Jul 25, 2024

πŸ”Ž Search Terms

number numeric branded types plus minus add subtract

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Nominal types (tagged, branded)

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAwgrgJwRAdgYxAHgCoD4oC8UKcAtgEYQJQBkUA3lAPpNqLLogBcU2UAvgG4AUKEhQAoogD2hWO1QZMAIggzluEWOgBVAMoAROfCSKsyuAGcAJhpHCAZnHTAAltJRQ0AQxQAFRDQAC29LCABJYAhSAAowBFc0CJQpBGkeVOkAGigAd28AGwKIYHCUfQMeCoBKBmEoBqgAeiaoADlZbSgqNIQc8jhgKHC86TgC6ygQgDdoCAAPSDQoyY8IesbkYERPfKKSsoqofHjE5MyRfmEgA

πŸ’» Code

type Currency<T> = number & { __currency: T };
type Euro = Currency<"euro">;
type USD = Currency<"usd">;

function canPurchaseItem(priceInEuro: Euro, walletInUSD: USD) {
    // No type error, but I would have expected one
    return walletInUSD > priceInEuro;
}

πŸ™ Actual behavior

Euro and USD -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:

  • one value that is an intersection of number and some object type
  • another value that is not assignable to that 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]:

// These are all type errors:
console.log([] + []);
console.log([] + {});
console.log([] + 1);
console.log({} + 1);
console.log({} + {});
console.log({ a: 1 } + { b: 2 });
@uhyo
Copy link
Contributor

uhyo commented Jul 25, 2024

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.

@jcalz
Copy link
Contributor

jcalz commented Jul 25, 2024

Even with nominal typing you'd need something like #42218 for it to have any effect on mathematical operators.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 29, 2024
@RGFTheCoder
Copy link

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 $/€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants