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

Add "indexed properties" #956

Closed
dmitryDemchenko opened this issue Oct 24, 2014 · 7 comments · May be fixed by Woodpile37/TypeScript#10
Closed

Add "indexed properties" #956

dmitryDemchenko opened this issue Oct 24, 2014 · 7 comments · May be fixed by Woodpile37/TypeScript#10
Labels
Question An issue which isn't directly actionable in code

Comments

@dmitryDemchenko
Copy link

In our project we use ActiveX objects accessible from JavaScript. I have .d.ts of COM library.
One of the external types has "indexed properties". As for now, it's TS declaration loks like

interface axType {
  Values(index:string):string;
}

JS code like

axObject.Values('F1') = 'test'

works just fine in runtime, but TS compiler shows the message: "Invalid left-hand side of assignment expression".

I can't see the way how to define that interface in .d.ts.

As I can imagine, it should be something like

class axType { 
  get Values(index:string): string 
  {
  //....
  }

  set Values(index:string, value: string)
  {
  // ...
  }
}
@RyanCavanaugh
Copy link
Member

This is already supported. Use an index signature:

interface axType {
  Values: { [index: string]: string };
}

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 24, 2014
@zspitz
Copy link
Contributor

zspitz commented Jun 30, 2016

@RyanCavanaugh Can I define a signature with multiple parameters? Or with types other than number or string?

@zspitz
Copy link
Contributor

zspitz commented Jul 5, 2016

@RyanCavanaugh Also, won't defining the indexers this way require the use of square brackets:

axObject.Values['F1'] = 'test';

but the OP's Javascript uses invocation:

axObject.Values('F1') = 'test'

This seems to be a valid (although relatively unusual) pattern in ActiveX libraries, e.g. Item property on Scripting.Dictionary:

var d = new ActiveXObject("Scripting.Dictionary");
d.Item("a") = "Athens";

@dmitryDemchenko
Copy link
Author

@zspitz as far as I can see JScript indexer is something not supported in ECMA; also TypeScript is intended to compile to JavaScript (ecma). That's why this kind of indexes definitely will not be supported in TypeScript.

In my project I ended up rewriting all calls like

d.Item("a") = "Athens"

into

AssignIndexedProperty(d, "a", " "Athens"")

and defining function

AssignIndexedProperty = new Function (d, name, value, "d(name) = value")

That trick worked good in JScript interpreter, but had terrible readability ((((

@zspitz
Copy link
Contributor

zspitz commented Jul 12, 2016

@dmitryDemchenko You may be interested to know that I've written a tool to convert type libraries to Typescript definition files. https://github.com/zspitz/ts-activex-gen

@dmitryDemchenko
Copy link
Author

dmitryDemchenko commented Jul 12, 2016

@zspitz Nice job! I did almost the same for our ActiveX libraries, or, to be more accurate, for .NET "interop" libraries. It was very similar, but I suppose a little bit easier thanks to "reflection" mechanism.

@zspitz
Copy link
Contributor

zspitz commented Jan 24, 2017

@dmitryDemchenko I originally was using .NET reflection, but there were too many mismatches between the .NET reflection model and the COM object model. I am currently using tlbinf32.dll.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants