microsoft / microsoft/TypeScript

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

Open
#42,553 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the motivating TypeScript examples and the linked TypeScript Playground reproduction. Investigate the type-printing and IDE-support behavior involved; done would mean equivalent intersections are presented in a clearer Extract-like form without changing runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.