Skip to content
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

Enable default properties #14

Closed
zspitz opened this issue Jan 22, 2017 · 3 comments
Closed

Enable default properties #14

zspitz opened this issue Jan 22, 2017 · 3 comments

Comments

@zspitz
Copy link
Owner

zspitz commented Jan 22, 2017

No description provided.

@zspitz
Copy link
Owner Author

zspitz commented Feb 22, 2017

Default properties cannot be used in Javascript, because there is no Set/Let distinction. The only possibility would be via some AST transformation.

@zspitz zspitz closed this as completed Feb 22, 2017
@zspitz zspitz reopened this Feb 6, 2018
@zspitz zspitz changed the title Handle default properties, if it is possible to access in Javascript Enable default properties Feb 7, 2018
@zspitz
Copy link
Owner Author

zspitz commented Feb 7, 2018

Automation objects have the concept of a default property -- a property that is accessed without actually calling the name of the property. When the property has parameters:

interface Dictionary<TKey = any, TItem = any> {
    Item(Key: TKey): TItem;
}

then it can be used from Javascript:

const dict: Dictionary<string, number> = new ActiveXObject('Scripting.Dictionary');
dict.Add('A', 1);
WScript.Echo(dict('A'));

and can be typed on an interface using a callable signature:

interface Dictionary<TKey = any, TItem = any> {
    Item(Key: TKey): TItem;
    (Key: TKey): TItem;
}

Conclusion: default properties with parameters can be modeled in definition files.


  • What happens when the default property has no parameters? There is an ambiguity:
// Person object has Name as default property
var person = new ActiveXObject('Person');
// will person contain the new instance of Person, or will it contain the value of the Name property?

Only interfaces can have callable signatures; classes cannot currently have them.

Currently we are using classes for Automation types, in order to use private members to prevent newing the type or assigning plain Javascript objects with a matching shape. So we cannot model default properties in this case.

However, the benefits of default properties outweigh the benefits of typing as a class, so when there is a default property we should type as interface with the default property.


In order to resolve this fully, one of three things could happen:

  • Interfaces would allow private members -- then we could use interfaces with their call signature for default properties, and a private member to enforce nominal typing
  • Interfaces would allow enforcing nominal typing directly; same as above, but no private member required
  • Classes would allow a callable signature

@zspitz zspitz closed this as completed in bbffd70 Feb 8, 2018
@zspitz
Copy link
Owner Author

zspitz commented Feb 13, 2018

There is a further issue: adding callable signatures means Typescript defines the interface as a function, with all a function's methods -- call, apply, constructor etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant