Skip to content

Commit 79fbbf4

Browse files
committed
improve TypeScript support. add simple test script.
1 parent 96fa07a commit 79fbbf4

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"build": "gulp",
2828
"docs": "jsdoc -c jsdoc.docs.json -R README.md",
2929
"pages": "node scripts/pages",
30-
"types": "jsdoc -c jsdoc.types.json && node scripts/types.js",
30+
"types": "jsdoc -c jsdoc.types.json && node scripts/types.js && tsc types/protobuf.js-test.ts --lib es2015 --noEmit",
3131
"lint": "eslint src",
3232
"test": "tape tests/*.js | tap-spec",
3333
"zuul": "zuul --ui tape --no-coverage --concurrency 1 -- tests/*.js",
@@ -62,6 +62,7 @@
6262
"tap-spec": "^4.1.1",
6363
"tape": "^4.6.3",
6464
"tsd-jsdoc": "dcodeIO/tsd-jsdoc",
65+
"typescript": "^2.2.0-dev.20161202",
6566
"vinyl-buffer": "^1.0.0",
6667
"vinyl-fs": "^2.4.4",
6768
"vinyl-source-stream": "^1.1.0",

scripts/types.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var dts = fs.readFileSync(path.join(dir, "types.d.ts"), "utf8");
1818
// Fix generic promises
1919
dts = dts.replace(/Promise\./g, "Promise");
2020

21+
// Fix classes
22+
dts = dts.replace(/\(\(\) => any\)/g, "any");
23+
2124
// Fix multidimensional arrays
2225
var found;
2326
do {
@@ -31,7 +34,7 @@ do {
3134
// Remove declare statements and wrap everything in a module
3235
dts = dts.replace(/\bdeclare\s/g, "");
3336
dts = dts.replace(/^/mg, " ");
34-
dts = header.join('\n')+"\ndeclare module protobuf {\n\n" + dts + "\n}\n";
37+
dts = header.join('\n')+"\ndeclare module \"protobufjs\" {\n\n" + dts + "\n}\n";
3538

3639
fs.writeFileSync(path.join(dir, "protobuf.js.d.ts"), dts);
3740
fs.unlinkSync(path.join(dir, "types.d.ts"));

src/inherits.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _TypeError = util._TypeError;
1818
/**
1919
* Inherits a custom class from the message prototype of the specified message type.
2020
* @param {Function} clazz Inheriting class
21-
* @param {Type} type Inherited message type
21+
* @param {Type|ReflectionObject} type Inherited message type
2222
* @param {InheritanceOptions} [options] Inheritance options
2323
* @returns {Prototype} Created prototype
2424
*/
@@ -40,7 +40,7 @@ function inherits(clazz, type, options) {
4040
*/
4141

4242
var classProperties = {
43-
43+
4444
/**
4545
* Reference to the reflected type.
4646
* @name Class.$type

types/protobuf.js-test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference path="./protobuf.js.d.ts" />
2+
import * as protobuf from "protobufjs";
3+
4+
export const proto = {"nested":{"Hello":{"fields":{"value":{"rule":"required","type":"string","id":1}}}}};
5+
6+
const root = protobuf.Root.fromJSON(proto);
7+
8+
export class Hello {
9+
constructor (properties: any) {
10+
protobuf.Prototype.call(this, properties);
11+
}
12+
}
13+
14+
protobuf.inherits(Hello, root.lookup("Hello"));

types/protobuf.js.d.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
/*
55
* protobuf.js v6.0.1 TypeScript definitions
6-
* Generated Fri, 02 Dec 2016 12:20:52 UTC
6+
* Generated Fri, 02 Dec 2016 15:01:02 UTC
77
*/
8-
declare module protobuf {
8+
declare module "protobufjs" {
99

1010
/**
1111
* Provides common type definitions.
@@ -306,7 +306,7 @@ declare module protobuf {
306306
* @returns {Promise<Root>|Object} A promise if callback has been omitted, otherwise the protobuf namespace
307307
* @throws {TypeError} If arguments are invalid
308308
*/
309-
function load(filename: (string|string[]), root?: Root, callback?: (() => any)): (Promise<Root>|Object);
309+
function load(filename: (string|string[]), root?: Root, callback?: any): (Promise<Root>|Object);
310310

311311
/**
312312
* Options passed to {@link inherits}, modifying its behavior.
@@ -324,11 +324,11 @@ declare module protobuf {
324324
/**
325325
* Inherits a custom class from the message prototype of the specified message type.
326326
* @param {Function} clazz Inheriting class
327-
* @param {Type} type Inherited message type
327+
* @param {Type|ReflectionObject} type Inherited message type
328328
* @param {InheritanceOptions} [options] Inheritance options
329329
* @returns {Prototype} Created prototype
330330
*/
331-
function inherits(clazz: (() => any), type: Type, options?: InheritanceOptions): Prototype;
331+
function inherits(clazz: any, type: (Type|ReflectionObject), options?: InheritanceOptions): Prototype;
332332

333333
/**
334334
* This is not an actual type but stands as a reference for any constructor of a custom message class that you pass to the library.
@@ -719,7 +719,7 @@ declare module protobuf {
719719
* @returns {Object} Prototype
720720
* @this ReflectionObject
721721
*/
722-
static extend(constructor: (() => any)): Object;
722+
static extend(constructor: any): Object;
723723

724724
/**
725725
* Converts this reflection object to its JSON representation.
@@ -1153,7 +1153,7 @@ declare module protobuf {
11531153
* @returns {Promise<Root>|undefined} A promise if `callback` has been omitted
11541154
* @throws {TypeError} If arguments are invalid
11551155
*/
1156-
load(filename: (string|string[]), callback?: (() => any)): (Promise<Root>|undefined);
1156+
load(filename: (string|string[]), callback?: any): (Promise<Root>|undefined);
11571157

11581158
}
11591159

@@ -1215,7 +1215,7 @@ declare module protobuf {
12151215
* @param {boolean} [responseDelimited=false] Whether response data is length delimited
12161216
* @returns {Object} Runtime service
12171217
*/
1218-
create(rpc: (() => any), requestDelimited?: boolean, responseDelimited?: boolean): Object;
1218+
create(rpc: any, requestDelimited?: boolean, responseDelimited?: boolean): Object;
12191219

12201220
}
12211221

@@ -1228,7 +1228,7 @@ declare module protobuf {
12281228
* @param {function(?Error, Uint8Array=)} callback Node-style callback called with the error, if any, and the response data
12291229
* @returns {undefined}
12301230
*/
1231-
function RPCImpl(method: Method, requestData: Uint8Array, callback: (() => any)): undefined;
1231+
function RPCImpl(method: Method, requestData: Uint8Array, callback: any): undefined;
12321232

12331233
/**
12341234
* Handle object returned from {@link tokenize}.
@@ -1240,11 +1240,11 @@ declare module protobuf {
12401240
* @property {function(string, boolean=):boolean} skip Skips a token, returns its presence and advances or, if non-optional and not present, throws
12411241
*/
12421242
interface TokenizerHandle {
1243-
line: (() => any);
1244-
next: (() => any);
1245-
peek: (() => any);
1246-
push: (() => any);
1247-
skip: (() => any);
1243+
line: any;
1244+
next: any;
1245+
peek: any;
1246+
push: any;
1247+
skip: any;
12481248
}
12491249

12501250

@@ -1369,7 +1369,7 @@ declare module protobuf {
13691369
* Defaults to use the internal constuctor.
13701370
* @returns {Prototype} Message instance
13711371
*/
1372-
create(properties?: Object, ctor?: (() => any)): Prototype;
1372+
create(properties?: Object, ctor?: any): Prototype;
13731373

13741374
/**
13751375
* Encodes a message of this type.
@@ -1496,7 +1496,7 @@ declare module protobuf {
14961496
* @param {Object|string[]} [scope] Function scope
14971497
* @returns {function} A function to apply the scope manually when `scope` is an array, otherwise the generated function with scope applied
14981498
*/
1499-
type CodegenEnder = (name?: string, scope?: (Object|string[])) => (() => any);
1499+
type CodegenEnder = (name?: string, scope?: (Object|string[])) => any;
15001500

15011501
/**
15021502
* Constructs new long bits.
@@ -1610,14 +1610,14 @@ declare module protobuf {
16101610
* If you assign any compatible buffer implementation to this property, the library will use it.
16111611
* @type {?Function}
16121612
*/
1613-
var Buffer: (() => any);
1613+
var Buffer: any;
16141614

16151615
/**
16161616
* Optional Long class to use.
16171617
* If you assign any compatible long implementation to this property, the library will use it.
16181618
* @type {?Function}
16191619
*/
1620-
var Long: (() => any);
1620+
var Long: any;
16211621

16221622
/**
16231623
* Converts a number or long to an 8 characters long hash string.
@@ -1680,7 +1680,7 @@ declare module protobuf {
16801680
* @param {...*} params Function arguments
16811681
* @returns {Promise<*>} Promisified function
16821682
*/
1683-
function asPromise(fn: (() => any), ctx: Object, params: any): Promise<any>;
1683+
function asPromise(fn: any, ctx: Object, params: any): Promise<any>;
16841684

16851685
/**
16861686
* Fetches the contents of a file.
@@ -1689,7 +1689,7 @@ declare module protobuf {
16891689
* @param {function(?Error, string=)} [callback] Node-style callback
16901690
* @returns {Promise<string>|undefined} Promise if callback has been omitted
16911691
*/
1692-
function fetch(path: string, callback?: (() => any)): (Promise<string>|undefined);
1692+
function fetch(path: string, callback?: any): (Promise<string>|undefined);
16931693

16941694
/**
16951695
* Tests if the specified path is absolute.
@@ -1811,7 +1811,7 @@ declare module protobuf {
18111811
* @param {number} val Value to write
18121812
* @returns {Writer} `this`
18131813
*/
1814-
push(fn: (() => any), len: number, val: number): Writer;
1814+
push(fn: any, len: number, val: number): Writer;
18151815

18161816
/**
18171817
* Writes a tag.

0 commit comments

Comments
 (0)