From 2016139475410827edcbbbcc17d6794e14c65bee Mon Sep 17 00:00:00 2001 From: Zev Spitz Date: Thu, 10 May 2018 16:37:44 +0200 Subject: [PATCH] Interop definitions - https://github.com/Microsoft/TypeScript/issues/21440 --- .../activex-interop/activex-interop-tests.ts | 49 ++++++++ types/activex-interop/index.d.ts | 113 ++++++++++++++++++ types/activex-interop/tsconfig.json | 21 ++++ types/activex-interop/tslint.json | 3 + 4 files changed, 186 insertions(+) create mode 100644 types/activex-interop/activex-interop-tests.ts create mode 100644 types/activex-interop/index.d.ts create mode 100644 types/activex-interop/tsconfig.json create mode 100644 types/activex-interop/tslint.json diff --git a/types/activex-interop/activex-interop-tests.ts b/types/activex-interop/activex-interop-tests.ts new file mode 100644 index 0000000000..e2ece7401e --- /dev/null +++ b/types/activex-interop/activex-interop-tests.ts @@ -0,0 +1,49 @@ +// copied from the definitions in activex-scripting +interface Dictionary { + /** Add a new key and item to the dictionary. */ + Add(Key: TKey, Item: TItem): void; + + /** Get the number of items in the dictionary. */ + readonly Count: number; + + /** Determine if a given key is in the dictionary. */ + Exists(Key: TKey): boolean; + HashVal(Key: TKey): any; + + /** Set or get the item for a given key */ + Item(Key: TKey): TItem; + + /** Get an array containing all items in the dictionary. */ + Items(): SafeArray; + + /** Change a key to a different key. */ + Key(Key: TKey): TKey; + + /** Get an array containing all keys in the dictionary. */ + Keys(): SafeArray; + + /** Remove a given key from the dictionary. */ + Remove(Key: TKey): void; + + /** Remove all information from the dictionary. */ + RemoveAll(): void; + + /** Set or get the item for a given key */ + (Key: TKey): TItem; +} + +interface ActiveXObjectNameMap { + 'Scripting.Dictionary': Dictionary; +} + +const dict: Dictionary = new ActiveXObject('Scripting.Dictionary'); +dict.Add('one', 1); +dict.Add('two', 2); +dict.Add('three', 3); + +const keyEnumerator = new Enumerator(dict.Keys()); +keyEnumerator.moveFirst(); +while (!keyEnumerator.atEnd()) { + const item = dict(keyEnumerator.item()); + const power = Math.pow(item, 2); +} diff --git a/types/activex-interop/index.d.ts b/types/activex-interop/index.d.ts new file mode 100644 index 0000000000..5b2a0acc42 --- /dev/null +++ b/types/activex-interop/index.d.ts @@ -0,0 +1,113 @@ +// Type definitions for Javascript Automation interop 0.0 +// Project: https://msdn.microsoft.com/en-us/library/ff521046(v=vs.85).aspx +// Definitions by: Zev Spitz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +// tslint:disable-next-line no-empty-interface +interface ActiveXObjectNameMap { } + +interface ActiveXObject { + new (progid: K): ActiveXObjectNameMap[K]; + new(s: string): any; +} +declare var ActiveXObject: ActiveXObject; + +/** + * Represents an Automation SAFEARRAY + */ +declare class SafeArray { + private constructor(); + private SafeArray_typekey: SafeArray; +} + +/** + * Allows enumerating over a COM collection, which may not have indexed item access. + */ +interface Enumerator { + /** + * Returns true if the current item is the last one in the collection, or the collection is empty, + * or the current item is undefined. + */ + atEnd(): boolean; + + /** + * Returns the current item in the collection + */ + item(): T; + + /** + * Resets the current item in the collection to the first item. If there are no items in the collection, + * the current item is set to undefined. + */ + moveFirst(): void; + + /** + * Moves the current item to the next item in the collection. If the enumerator is at the end of + * the collection or the collection is empty, the current item is set to undefined. + */ + moveNext(): void; +} + +interface EnumeratorConstructor { + new (collection: SafeArray | { Item(index: any): T }): Enumerator; + new (collection: any): Enumerator; +} + +declare var Enumerator: EnumeratorConstructor; + +/** + * Enables reading from a COM safe array, which might have an alternate lower bound, or multiple dimensions. + */ +interface VBArray { + /** + * Returns the number of dimensions (1-based). + */ + dimensions(): number; + + /** + * Takes an index for each dimension in the array, and returns the item at the corresponding location. + */ + getItem(dimension1Index: number, ...dimensionNIndexes: number[]): T; + + /** + * Returns the smallest available index for a given dimension. + * @param dimension 1-based dimension (defaults to 1) + */ + lbound(dimension?: number): number; + + /** + * Returns the largest available index for a given dimension. + * @param dimension 1-based dimension (defaults to 1) + */ + ubound(dimension?: number): number; + + /** + * Returns a Javascript array with all the elements in the VBArray. If there are multiple dimensions, + * each successive dimension is appended to the end of the array. + * Example: [[1,2,3],[4,5,6]] becomes [1,2,3,4,5,6] + */ + toArray(): T[]; +} + +interface VBArrayConstructor { + new (safeArray: SafeArray): VBArray; +} + +declare var VBArray: VBArrayConstructor; + +/** + * Automation date (VT_DATE) + */ +declare class VarDate { + private constructor(); + private VarDate_typekey: VarDate; +} + +interface DateConstructor { + new(vd: VarDate): Date; +} + +interface Date { + getVarDate: () => VarDate; +} diff --git a/types/activex-interop/tsconfig.json b/types/activex-interop/tsconfig.json new file mode 100644 index 0000000000..49c7698384 --- /dev/null +++ b/types/activex-interop/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ "../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "activex-interop-tests.ts" + ] +} \ No newline at end of file diff --git a/types/activex-interop/tslint.json b/types/activex-interop/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/activex-interop/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file