microsoft / microsoft/TypeScript
Don't allow math operations on different branded numeric types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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 });
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l'esempio Playground fornito e confrontalo con l'esempio mismatched-operator collegato per comprendere il comportamento attuale dei controlli. Il lavoro è completato quando le operazioni tra intersezioni numeriche branded distinte producono gli errori di tipo previsti senza introdurre regressioni negli errori mismatched-operator esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100