microsoft / microsoft/TypeScript

Better printing of `T & U` where this is equivalent to `Extract<T, U>`

Abierto
#42,553 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

🔍 Search Terms

Extract intersection union ide editor printing

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

There are many cases where T & U is equivalent to Extract<T, U>. However, the former has much worse type printing in these cases than the latter, leading to a much worse developer experience for this case.

I initially raised this as an eslint rule, but it would be very difficult to do do from that side (as explained in the comments of https://github.com/typescript-eslint/typescript-eslint/issues/2983

📃 Motivating Example

type A = Action & { type: 'action1' | 'action2' } 
// ({
//     type: 'action1';
//     a: number;
// } & {
//     type: 'action1' | 'action2';
// }) | ({
//     type: 'action2';
//     b: string;
// } & {
//     type: 'action1' | 'action2';
// })

type B = Extract<Action, { type: 'action1' | 'action2' }>
// {
//     type: 'action1';
//     a: number;
// } | {
//     type: 'action2';
//     b: string;
// }

https://www.typescriptlang.org/play?#code/C4TwDgpgBAggxsAlgewHZQLwCgoB8oDeUokAXFAOQCGCKqAjBQDRRXmoCuAtgEYQBOUAL458REhHLVaaAEzMoPcgGdg-RKgDmw0YWLhJlGkjQBmBXBVqN2kViwTYmWDPQAyPRKnG6jPEdd5YSgHAygAIWcAUQAPNWMAHngTVBZxA29XP3xpFKChAD4gA

💻 Use Cases

Improve developer experience, error messages, and IDE support.

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 los ejemplos motivadores de TypeScript y la reproducción vinculada en TypeScript Playground. Investiga el comportamiento de la impresión de tipos y del soporte del IDE; se considerará terminado cuando las intersecciones equivalentes se presenten en una forma Extract-like más clara sin cambiar el comportamiento en tiempo de ejecución.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
developer-experience
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.