microsoft / microsoft/TypeScript
Don't allow math operations on different branded numeric types
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔎 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 });
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el ejemplo de Playground proporcionado y compáralo con el ejemplo de mismatched-operator enlazado para comprender el comportamiento actual de comprobación. El trabajo estará terminado cuando las operaciones entre intersecciones numéricas branded distintas produzcan los errores de tipo esperados sin introducir regresiones en los errores de mismatched-operator existentes.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100