vue icon indicating copy to clipboard operation
vue copied to clipboard

Allow inferring types from Vue.set(object, ...) pattern

Open NikhilVerma opened this issue 2 years ago • 1 comments

Right now it's not possible to infer the type when doing Vue.set this is because Vue assumes it to be a generic object. But with a small change we can allow these types to be inferred in a clear and consistent way similar to how it's array inference works. As an added benefit it also allows autocomplete of Vue.set().

This does however disallow setting brand new properties on objects. Although setting new properties on objects/classes which aren't previously defined in Typescript can be an anti-pattern.

Playground link

What kind of change does this PR introduce? (check at least one)

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update
  • [ ] Refactor
  • [ ] Build-related changes
  • [ ] Other, please describe:

Does this PR introduce a breaking change? (check one)

  • [x] Yes
  • [ ] No

If yes, please describe the impact and migration path for existing applications:

This PR adds better type inference for Vue.set(object...) pattern, this might cause issues for existing consumers which have invalid usage of Vue.set. However I believe this is a good thing as it will allow them to fix those issues in Typescript.

The PR fulfills these requirements:

  • [x] It's submitted to the dev branch for v2.x (or to a previous version branch), not the master branch
  • [ ] When resolving a specific issue, it's referenced in the PR's title (e.g. fix #xxx[,#xxx], where "xxx" is the issue number)
  • [ ] All tests are passing: https://github.com/vuejs/vue/blob/dev/.github/CONTRIBUTING.md#development-setup
  • [ ] New/updated tests are included

If adding a new feature, the PR's description includes:

  • [ ] A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

NikhilVerma avatar Mar 23 '22 16:03 NikhilVerma

@yyx990803 Can you please consider this? I think this offers a significant improvement in type safety of Vue.set

NikhilVerma avatar Jul 07 '22 09:07 NikhilVerma