microsoft / microsoft/TypeScript

Common fields of union not resolved for generic conditional with `any` branch

Abierto
#62,466 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: Conditional Types Help Wanted
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

### 🔎 Search Terms

"conditional generics with any branch in union", "union with generic conditional branch"

### 🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional generics used in unions but didn't find anything that seemed relevant.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.2#code/C4TwDgpgBAkgdgYQPYFsVLlAvFA3gY1XTgC4oBnYAJwEs4BzAXwG4AoV0SKZOAExuA0MAQwA2AHgDSUCAA9gEPuQrU69KAB8ocAK4oARhCoA+bFGlyFS7XsNUoAfjwYIZXQaOMoZAEoRCVLzilLQMADRQOnAA1nBIAO5wxmyc0ACqcEJwAOoCABZ+9DqiwlQ8-IIiEhbyirzKIWqaNh4mZvDIaBjNABTlAlliUqYAZLCIRBgAlGwAZlH4lZhywihgohAZWbnABRBFJVRSMrXWjQzN7nbGPdEQIGSSU3isUG9QhHCUkZkYO3sHUr9JZiMy4LzCZRbP75QrFIEYCqDarJV7vAD06KguxoyniSCo0WUkJOkEWEF4aLeUW2sP28LKiIGVQAdIQunA2Ix2KluEyQaJ-gBBOAgY6WOoNVQXLRXIymHA1Kz1Fp2RxQYSi7xQPwBILnegRKKxBJJFLgdK-HL5EUgYHI8WnFUGy62eXtCYc3r2qrC0XDKBjDqTOAzVjzOCLLInVbrTZWv1ipWSlShdSyt0mW73R7PXBUj4Yb40mG7W0+uCgnDgjVQhM20UVoaSVHvKCY7F5XFQPIk4TYi0yKhUAlQWajgAG7OIE+0SGAJ1xggu3QnTcFDaTo3GnRnBZL1rLjf5yLZIbYbYLHf0OgXu0Une7ACpeEgIOQn1B8YSC59vmARzARN1zBCFlD6E9fU3AMg09YgwzbACkCAzd1zPDkuVYIA

### 💻 Code

```ts
type InCommon = {common: string};

type Conditional = K extends number ? {one: number} : Record;
type UnionWithRegularConditional = InCommon | (Conditional & InCommon);
function exampleUnionWithRegular(key: K) {
const unionWithRegularConditional = {} as UnionWithRegularConditional;
// this works as expected
unionWithRegularConditional.common;
}

type ConditionalWithAny = K extends number ? any : Record;
type UnionWithAnyConditional = InCommon | (ConditionalWithAny & InCommon);
function exampleUnionWithAny(key: K) {
const unionWithAnyConditional = {} as UnionWithAnyConditional;
// this has a type error for `common` not existing on `ConditionalWithAny & InCommon`
unionWithAnyConditional.common;

// but then this *does* work
const propWithAnyConditional = {} as (ConditionalWithAny & InCommon);
propWithAnyConditional.common;
}
```

### 🙁 Actual behavior

When a union type contains a conditional generic whose branch is `any`, the common fields of the union are not being correctly recognized.

This is illustrated above when TS is unable to recognize that `common` is a field on `unionWithAnyConditional`.

### 🙂 Expected behavior

The expected behavior here is that common fields of the union should be correctly resolved.

From the above example, the expectation is that `unionWithAnyConditional.common` would not have a "does not exist on type" error.

### Additional information about the issue

_No response_

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la reproducción enlazada de TypeScript Playground y confirma la diferencia entre el condicional normal y el condicional con una rama `any`. Rastrea cómo el compilador gestiona las propiedades comunes de la unión resultante y añade cobertura para `unionWithAnyConditional.common`. Se considera terminado cuando el acceso esperado ya no informa de que falta `common`.

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
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.