wix-incubator / corvid-types

Type definitions for Corvid by Wix
MIT License
5 stars 3 forks source link

Variable's property as selector throw an error #11

Closed moshfeu closed 4 years ago

moshfeu commented 4 years ago

For example

Working

const selector = '#image152';
$w(selector); 

Not working

const selector = {
  foo: '#image152'
};
$w(selector.foo)

Throwing

Argument of type '{ foo: string; }' is not assignable to parameter of type...

agankarin commented 4 years ago

Hey @moshfeu! This issue is related to TS compiler. The compiler expects here a specific string(one of the component's nicknames or selector by type). If you try to pass any argument that can be changed during runtime(in the above example, you can change foo to be any an array for example), the compiler can't be sure what would be the value during runtime, and this is the reason why it raised an error.

If we would change the $w function declaration to accept any string(won't solve your case), we would lose the string value validation(a valid nickname that indeed exists on your page). This feature is much more valuable to our users.

Our recommendation is to assign the selector string to a const, this way the compiler can run the type checker.

moshfeu commented 4 years ago

Makes sense. Thanks!

agankarin commented 4 years ago

@moshfeu BTW, in the online IDE, we try to hide those error, but we can't control your local IDE :)

moshfeu commented 4 years ago

True :)