Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quick fix support for all valid strings for computed properties #46402

Closed
5 tasks done
TrevorSayre opened this issue Oct 17, 2021 · 0 comments · Fixed by #46716
Closed
5 tasks done

Add quick fix support for all valid strings for computed properties #46402

TrevorSayre opened this issue Oct 17, 2021 · 0 comments · Fixed by #46716
Labels
Bug A bug in TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". Help Wanted You can do this
Milestone

Comments

@TrevorSayre
Copy link

TrevorSayre commented Oct 17, 2021

Suggestion

Add a quick fix for TypeScript computed properties to "Add missing properties" that supports all valid strings.

🔍 Search Terms

add missing properties
declare missing properties
implement all members

✅ 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

type Letters = 'a' | 'b' | 'c' | 'd' | 'e';
type Numbers = 1 | 2 | 3;
type Chars = '$' | '_';

type Names = `${Letters}${Uppercase<Letters>}${Numbers}${Chars}`;

// Quick fix available
// so computed properties, transforms, and long lists work
const names: { [K in Names]: K } = {};
type Letters = 'a' | 'b' | 'c' | 'd' | 'e';
type Numbers = 1 | 2 | 3;
type Chars = '@' | '!'; // Difference is only this line

type Names = `${Letters}${Uppercase<Letters>}${Numbers}${Chars}`;

// Quick fix not available
const names: { [K in Names]: K } = {};

The last example works in WebStorm via "implement all members" and would be a welcome addition to VSCode. The quick fix for #28491 was implemented via #44576 and solved some use cases, but not this one.

The error output does already suggest all the missing properties. It seems like the data is available to execute a quick fix.

image

image

Such names should be usable:

image

Additionally, when the quick fix does work, it only adds the properties and sets values as undefined even when the value is known (as in the above examples).

image

image

This issue was inspired by this tweet:
https://twitter.com/majidrazvi/status/1449493443743391746

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". Help Wanted You can do this labels Nov 4, 2021
@DanielRosenwasser DanielRosenwasser added this to the Backlog milestone Nov 4, 2021
@DanielRosenwasser DanielRosenwasser added the Domain: Quick Fixes Editor-provided fixes, often called code actions. label Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. Effort: Casual Good issue if you're already used to contributing to the codebase. Harder than "good first issue". Help Wanted You can do this
Projects
None yet
2 participants