microsoft / microsoft/TypeScript
union of pattern template literals intersected with optional brand mutually assignable to one with incompatible brand
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
🔎 Search Terms
brand, flavor, pattern template literal, placeholder, union, intersection, optional, assignable
🕗 Version & Regression Information
- This changed between versions 4.3.0-dev.20210330 and 4.3.0-dev.20210331
- This changed in commit or PR #43440, I think
⏯ Playground Link
💻 Code
type TmpLitUnion = `0${string}` | `1${string}`
type BrandX = TmpLitUnion & { a?: "x" };
type BrandY = TmpLitUnion & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- no error
🙁 Actual behavior
BrandX and BrandY are mutually assignable, even though they have an incompatible optional a property.
🙂 Expected behavior
There should be an error when trying to assign a BrandY to a BrandX or vice versa.
Additional information about the issue
This is from a Stack Overflow question. Looks like optional brands (this is called... "flavored" I guess?) don't distinguish between unions-of-pattern-template-literals. If you get rid of the union, you get the expected error:
type TmpLit = `0${string}`;
type BrandX = TmpLit & { a?: "x" };
type BrandY = TmpLit & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error
Or if you add something else to the union, you get the expected error:
type TmpLitUnionOrSomethingElse = `0${string}` | `1${string}` | "somethingElse"
type BrandX = TmpLitUnionOrSomethingElse & { a?: "x" };
type BrandY = TmpLitUnionOrSomethingElse & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error
Or if the brand is not optional, etc.
Not sure if this is a bug or a design limitation, or somehow intentional.
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
Reproduce la asignación en el TypeScript Playground enlazado y compárala después con los ejemplos sin unión y sin marca opcional del issue. Investiga la asignabilidad de uniones de literales de plantilla de patrón intersectadas con propiedades opcionales; se considera terminado cuando BrandX y BrandY ya no sean asignables mutuamente y los casos de comparación indicados sigan siendo correctos.
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
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 52/100