microsoft / microsoft/TypeScript

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

Offen
#42,553 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den motivierenden TypeScript-Beispielen und der verlinkten TypeScript Playground-Reproduktion. Untersuche das Verhalten der Typausgabe und der IDE-Unterstützung; als abgeschlossen gilt die Aufgabe, wenn äquivalente Schnittmengen in einer klareren Extract-like-Form dargestellt werden, ohne das Laufzeitverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
developer-experience
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.