Subscribe on changes!

Types `inject` does not support number argument

avatar
Oct 20th 2023

Vue version

3.3.5

Link to minimal reproduction

https://play.vuejs.org/#eNp9UctOwzAQ/BXLlxYpSlT1VgISoB7gAAg4+hIl2+KS2Ja9CZWi/Du7bpMW8YgUyTszux7v9PLGubRrQa5kHkqvHYoA2LprZXTjrEfRC+dtpytIhIeNGMTG20bMqGc2ae5s4454mnHBI2eXyihzbJ4vErFYXCiTZ4dr6AIqEBpXFwhUCZHHMRmd82wiZCIxlNZs9DbdBWvIaM9iJUtS6xr8k0NtTVByJSLDXFHX9vMhYuhbSEa8fIfy4xd8F/aMKfnsIYDvQMmJw8JvAQ/0+vUR9nSeyMZWbU3qf8gXCLZu2eNBdtuaimyf6aLb+7hKbbZvYb1HMGF8FBtl5RD1StJqeVF/Pf1kd5kuY58yA21xjOVn0v4sa212UOL3lInnn0IIKIpCXB1Vc8qT8FOiXJwSpYod5ZXupoK/vuchwzAJslFxHvvwBbtN3dI=

Steps to reproduce

See Comp.vue file

What is expected?

Inject to not give typescript error if a number is passed

What is actually happening?

getting the error

System Info

No response

Any additional comments?

provide seems to accept number as key, the parameter will be converted to a string so

provide(1, 42)

inject(1) // 42

inject('1') // 42
avatar
Dec 1st 2023

I think provide should not accept number as key.

If we should type convert is acceptable. How about allow bigint, Array of number, Array of int and so on?

avatar
Dec 1st 2023

@xiaoxiangmoe provide already allows number but inject does not.

How about allow bigint, Array of number, Array of int and so on?

We should only support PropertyKey ts type.

avatar
Dec 2nd 2023

Since this is a key for fetching data from an object, I don't think it's a good idea to get/set the same data from the key that is different types but has the same value.

inject(1) === inject('1')

So for TypeScript, I think we'd better disallow the number type.

avatar
Dec 2nd 2023

We have 2 solutions, either support number or drop number support from provide/inject.

For me either would be alright

avatar
Dec 2nd 2023

I agree with dropping number support.