vuejs / core

🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
https://vuejs.org/
MIT License
47.9k stars 8.36k forks source link

Types `inject` does not support number argument #9439

Open pikax opened 1 year ago

pikax commented 1 year ago

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
xiaoxiangmoe commented 1 year ago

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?

pikax commented 1 year ago

@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.

sxzz commented 1 year ago

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.

pikax commented 1 year ago

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

For me either would be alright

sxzz commented 1 year ago

I agree with dropping number support.