microsoft / microsoft/TypeScript

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

Aperta
#42,553 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experience Enhancement Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi motivanti in TypeScript e la riproduzione collegata in TypeScript Playground. Analizza il comportamento della stampa dei tipi e del supporto dell’IDE; il lavoro sarà completo quando le intersezioni equivalenti saranno presentate in una forma Extract-like più chiara senza modificare il comportamento a runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.