microsoft / microsoft/TypeScript
Don't allow math operations on different branded numeric types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 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
💻 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
numberand 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 });
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem bereitgestellten Playground-Beispiel und vergleiche es mit dem verlinkten mismatched-operator-Beispiel, um das aktuelle Prüfverhalten zu verstehen. Die Arbeit ist abgeschlossen, wenn Operationen zwischen unterschiedlichen gebrandeten numerischen Schnittmengen die erwarteten Typfehler erzeugen, ohne die bestehenden mismatched-operator-Fehler zu verschlechtern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100