mobx-vue | Vue bindings for MobX | State Container library
kandi X-RAY | mobx-vue Summary
kandi X-RAY | mobx-vue Summary
Vue bindings for MobX
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of mobx-vue
mobx-vue Key Features
mobx-vue Examples and Code Snippets
Community Discussions
Trending Discussions on mobx-vue
QUESTION
I'm currently debugging some libraries' code and for the life of me don't understand a specific error.
Here is a simplified example of the code I'm debugging:
...ANSWER
Answered 2020-Dec-11 at 11:14You have found an inconsistency in the way TypeScript deals with the interaction of Function
and union types.
When call
is called, the compiler knows test1Fnc
is either:
- a JavaScript
Function
object of some unknown signature, or - a JavaScript
Function
object which is called on a string (ie it has a string as itsthis
argument) and returns an object.
The compiler then asks if the call is valid for each of those types.
When it asks if the call is valid for type 1, it concludes that the call is not because test1Fnc
may not be callable on a string.
This is odd because this is contrary to regular TypeScript behaviour for Function
objects, where it normally allows any kind of function call. To see that such calls are allowed, temporarily change the initial type declaration to type Test1Type = object
and the error disappears.
This happens because two things about how Function
types are processed by the compiler are true:
- Any kind of call of a function on them is permissible, but
- They are not assignable to a function with specified arguments.
When the compiler knows test1Fnc
is a Function
only (as when the type declaration is changed), it works because of rule 1. But when the union type is involved, it first tries to assign the union type to the given signature of the call
type. That assignment fails because of rule 2 before rule 1 has had a chance to override it.
You can see this is not the intended behaviour because if Test1Type
is defined to be just either one of those two types (provided the modification explained in the next section is made) then the compiler does not give any errors. This is not right: if a path through the program is valid for either type in a union type, then the program should be valid with the union type.
As another observation, even if test1Fnc
is type 2 from above, it would not not be callable with the call
function set out. The given call
function expects a this
value of string
and also one regular argument of type string
, yet type 2 from above is just a this
value of type string
with no regular arguments. The compiler is not showing this error at present, but if you change the initial type declaration to type Test1Type = ((this: V) => object)
then you will see a different error produced. In turn this is eliminated by changing the line to type Test1Type = ((this: V, arg: V) => object)
.
To deal with this, you might consider one or more of the following:
- Redefine
Test1Type = object | ((this: V, arg: V) => object)
, which appears to be what the code is expecting. This doesn't really change the type given it could still be any object, but it does express the code's intention better - Then to clear the error, if you can cast the type of
test1Fnc
within theif
block to the expected type:res = (test1Fnc as (this: string, arg: string) => object).call(x,x)
. If you are unable to make a change like this, you might have to give up on any type safety and go fortype Test1Type = object
- You might like to report this an issue on the TypeScript GitHub. I'm not sure it's a priority for them as the use of
Function
objects for typing are probably not encouraged, and - I'd discourage the use of this union type anyhow. It's so general it's not really achieving type safety in any case. It's not clear how much control you have over the code, and what other values
Test1Type
might need to be, but if you can make wider changes, it would be preferable to write it as a union of more explicit types so the compiler could choose between them, or more rigorously set up a class hierarchy in full OOP style.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mobx-vue
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page