microsoft / microsoft/TypeScript
Better printing of `T & U` where this is equivalent to `Extract<T, U>`
Nobody has claimed this yet.
- 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;
// }
💻 Use Cases
Improve developer experience, error messages, and IDE support.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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