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 simulated nominal type for COM SAFEARRAY and VarDate to lib.scripthost.d.ts #17526

Closed
zspitz opened this issue Jul 31, 2017 · 3 comments
Closed
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@zspitz
Copy link
Contributor

zspitz commented Jul 31, 2017

TypeScript Version: nightly (2.5.0-dev.20170725)

When running Javascript under an Automation context (Windows Script Host, or HTML Applications), there are two types exposed by various APIs: VarDate and SafeArray. These types have no shape from Javascript's perspective:

  • they cannot be constructed using new, and
  • they have no members that can be consumed from Javascript

However, other methods/constructors/properties expect these types and will fail if an incorrect type is passed in:

var rst = new ActiveXObject('ADODB.Recordset');
// open the connection and recordset here
var fields = ['firstname', 'lastname'];
var values = ['Plony', 'Almony'];

// Will fail at runtime, since two SafeArrays are expected, and not two Javascript arrays
rst.Update(fields, values);

Currently, VarDate is typed in lib.scripthost.d.ts as an empty interface; while SafeArray has no defined type, and methods that expect a SafeArray are typed with any (example).

I propose adding a representation of these types to lib.scripthost.d.ts:

declare class VarDate {
    private VarDate_typekey: VarDate;
    private constructor();
}

declare class SafeArray<T = any> {
    private SafeArray_typekey: SafeArray<T>;
    private constructor():
}
  • An interface couldn't be used here, because there is no shape from Javascript, and an empty interface is equivalent to any

  • The private constructor prevents new VarDate();

  • The other private member prevents assigning an arbitrary object to a variable/parameter of this type:

      //compiler error -- Property 'VarDate_typekey' is missing in type '{}'
     let x: VarDate = {};
    

Note that this is actually simulating nominal typing (thanks @Aleksey-Bykov for the suggestion). If some form of nominal typing is introduced to TypeScript, the definitions for VarDate and SafeArray could be rewritten accordingly.


The previous definition for Update could then be written as follows:

declare namespace ADODB {
    interface Recordset {
        Update(Fields: SafeArray<string>, Values: SafeArray): void;
    }
}

VBArray and Enumerator could be modified to take advantage of the generic type of SafeArray, as in this commit.

@zpdDG4gta8XKpMCd
Copy link

that article is a bit outdated, here is the latest workaround: #202 (comment)

@DanielRosenwasser DanielRosenwasser added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 1, 2017
@zspitz
Copy link
Contributor Author

zspitz commented Aug 2, 2017

@Aleksey-Bykov Fixed, thanks.

@zspitz zspitz changed the title Add COM SAFEARRAY representation to lib.scripthost.d.ts Add simulated nominal type for COM SAFEARRAY and VarDate to lib.scripthost.d.ts Aug 3, 2017
zspitz added a commit to zspitz/TypeScript that referenced this issue Aug 3, 2017
@zspitz
Copy link
Contributor Author

zspitz commented Sep 11, 2017

@DanielRosenwasser Any news? Could you describe some of the discussion relating to this?

@mhegazy mhegazy added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Sep 19, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 20, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Sep 20, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants