question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type checks by typescript fail on Vue.set/delete

See original GitHub issue

Version

2.5.17

Reproduction link

https://github.com/nel215/vue-sandbox

Steps to reproduce

$ git clone https://github.com/nel215/vue-sandbox.git
$ cd vue-sandbox
$ yarn
$ ./node_modules/.bin/tsc index.ts
index.ts:5:9 - error TS2345: Argument of type '{}' is not assignable to parameter of type '"a"[]'.
  Property 'length' is missing in type '{}'.

5 Vue.set(data.obj, 1, 'a')
          ~~~~~~~~

index.ts:6:12 - error TS2345: Argument of type '{}' is not assignable to parameter of type '{}[]'.
  Property 'length' is missing in type '{}'.

6 Vue.delete(data.obj, 1)

What is expected?

Type checks by typescript succeed on this case.

What is actually happening?

Type checks by typescript fail on this case.


Can we use number as key of object on Vue.set/delete? If it’s yes, I will create a PR which fixes types/vue.d.ts.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
posvacommented, Aug 25, 2018

In js, the conversion is implicit but if you want to use strict typing, then you should cast your number into a string. Doing a[1] or a[‘1’]` in js will implicitly access the same thing. That being said, typescript allows it without warning, so I think we should support it too

const a: Record<string, boolean> = {}
a['1'] = false
a[2] = true
1reaction
KaelWDcommented, Aug 25, 2018

Note that the inverse is not true for arrays:

const a: Record<string, boolean> = {}
a['1'] = false
a[2] = true

const b: boolean[] = []
b['1'] = false  // Element implicitly has an 'any' type because index expression is not of type 'number'
b[2] = true

Could also go with fully strict instead, but that might annoy a few people

declare function set<T extends O[K], K extends keyof O, O>(object: O, key: K, value: T): T

const c: Record<string, boolean> = {}
const d: boolean[] = []
const e: { foo: 'bar' | 'baz' } = { foo: 'bar' }

set(c, '1', false)
set(c, 2, false)          // nope
set(c, '3', 'a string')   // nope

set(d, '1', false)        // nope
set(d, 2, false)
set(d, 3, 'a string')     // nope

set(e, '1', false)        // nope
set(e, 2, false)          // nope
set(e, 'foo', 'a string') // nope
set(e, 'bar', 'baz')      // nope
set(e, 'foo', 'baz')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Inconsistent Vue.set() and Vue.delete() TypeScript types #9353
As mentioned in #9347, there are certain cases right now where Vue types for Vue.set() and Vue.delete() work unexpectedly: Setting the value ...
Read more >
Vue.js 2: Delete property from data object - Stack Overflow
The answer is: Vue.delete(users, 'foo');. It took me a while to find it, that's why I'm posting it here ;-)
Read more >
strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is false , null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.
Read more >
Using Vue with TypeScript - Vue.js
With a Vite-based setup, the dev server and the bundler are transpilation-only and do not perform any type-checking. This ensures the Vite dev...
Read more >
ts-loader - npm
It performs type checking in a separate process with ts-loader ... The build should fail on TypeScript compilation errors as of webpack 2....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found