-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Type checks by typescript fail on Vue.set/delete #8707
Comments
Shouldn't it be |
Using number as key of Object is not recommended? In my actual case of |
I'm not sure that allowing such implicit type conversion in typings. I think it's enough to convert the id to string by using |
In js, the conversion is implicit but if you want to use strict typing, then you should cast your number into a string. Doing const a: Record<string, boolean> = {}
a['1'] = false
a[2] = true |
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') |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Closed via #8709 |
Version
2.5.17
Reproduction link
https://github.com/nel215/vue-sandbox
Steps to reproduce
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.
The text was updated successfully, but these errors were encountered: