Skip to content

Commit a7d2324

Browse files
committed
New: Attempt to improve TypeScript support by using explicit exports
1 parent f625eb8 commit a7d2324

20 files changed

Lines changed: 3778 additions & 3702 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,23 @@ There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf
237237

238238
### Usage with TypeScript
239239

240-
```ts
241-
/// <reference path="node_modules/protobufjs/types/protobuf.js.d.ts" />
240+
Under node.js:
242241

242+
```ts
243243
import * as protobuf from "protobufjs";
244244
...
245245
```
246246

247+
In the browser:
248+
249+
```ts
250+
/// <reference path="path/to/protobuf/index.d.ts" />
251+
import * as protobuf from "path/to/protobuf.js";
252+
...
253+
```
254+
255+
If you need long support, there is also a [TypeScript definition](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/long/index.d.ts) for that (on npm: [@types/long](https://www.npmjs.com/package/@types/long)).
256+
247257
See also: [Generating your own TypeScript definitions](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules)
248258

249259
Module Structure
@@ -453,7 +463,7 @@ Building the documentation to `docs/`:
453463
$> npm run docs
454464
```
455465

456-
Building the TypeScript definition to `types/`:
466+
Building the TypeScript definition to `index.d.ts`:
457467

458468
```
459469
$> npm run types

cli/pbts.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ exports.main = function(args, callback) {
9292
}
9393

9494
var output = [
95-
"// $> pbts " + process.argv.slice(2).join(" "),
95+
"// $> pbts " + args.join(" "),
9696
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
9797
""
9898
];
99-
if (argv.name !== "protobufjs")
99+
if (argv.name !== "protobuf")
100100
output.push(
101101
"import * as $protobuf from \"protobufjs\";",
102102
""
@@ -108,6 +108,8 @@ exports.main = function(args, callback) {
108108
fs.writeFileSync(argv.out, output);
109109
else
110110
process.stdout.write(output, "utf8");
111+
if (callback)
112+
callback(null);
111113
} catch (err) {
112114
if (callback)
113115
callback(err);
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
2-
// Generated Fri, 30 Dec 2016 12:21:10 UTC
1+
// $> pbts --name protobuf --out index.d.ts src
2+
// Generated Fri, 30 Dec 2016 15:32:08 UTC
33

4-
declare module "protobufjs" {
4+
export = protobuf;
5+
export as namespace protobuf;
6+
7+
declare namespace protobuf {
58

69
/**
710
* Constructs a class instance, which is also a message prototype.
@@ -1358,7 +1361,7 @@ declare module "protobufjs" {
13581361
* Streaming RPC helpers.
13591362
* @namespace
13601363
*/
1361-
module rpc {
1364+
namespace rpc {
13621365

13631366
/**
13641367
* Constructs a new RPC service instance.
@@ -1676,7 +1679,7 @@ declare module "protobufjs" {
16761679
* Common type constants.
16771680
* @namespace
16781681
*/
1679-
module types {
1682+
namespace types {
16801683

16811684
/**
16821685
* Basic type wire types.
@@ -1896,7 +1899,7 @@ declare module "protobufjs" {
18961899
* Various utility functions.
18971900
* @namespace
18981901
*/
1899-
module util {
1902+
namespace util {
19001903

19011904
/**
19021905
* Returns a promise from a node-style callback function.
@@ -1913,7 +1916,7 @@ declare module "protobufjs" {
19131916
* @memberof util
19141917
* @namespace
19151918
*/
1916-
module base64 {
1919+
namespace base64 {
19171920

19181921
/**
19191922
* Calculates the byte length of a base64 encoded string.
@@ -2133,7 +2136,7 @@ declare module "protobufjs" {
21332136
* @memberof util
21342137
* @namespace
21352138
*/
2136-
module path {
2139+
namespace path {
21372140

21382141
/**
21392142
* Tests if the specified path is absolute.
@@ -2299,7 +2302,7 @@ declare module "protobufjs" {
22992302
* @memberof util
23002303
* @namespace
23012304
*/
2302-
module utf8 {
2305+
namespace utf8 {
23032306

23042307
/**
23052308
* Calculates the UTF8 byte length of a string.

lib/tsd-jsdoc/publish.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ exports.publish = function publish(taffy, opts) {
6969

7070
// wrap everything in a module if configured
7171
if (options.module) {
72-
writeln("declare module ", JSON.stringify(options.module), " {");
72+
writeln("export = ", options.module, ";");
73+
writeln("export as namespace " + options.module, ";");
74+
writeln();
75+
writeln("declare namespace ", options.module, " {");
7376
++indent;
7477
}
7578

@@ -312,7 +315,7 @@ function handleElement(element, parent) {
312315
// handles (just) a namespace
313316
function handleNamespace(element, parent) {
314317
begin(element);
315-
writeln("module ", element.name, " {");
318+
writeln("namespace ", element.name, " {");
316319
++indent;
317320
getChildrenOf(element).forEach(function(child) {
318321
handleElement(child, element);
@@ -378,7 +381,7 @@ function handleClass(element, parent) {
378381
if (innerClasses.length) {
379382
writeln("");
380383
begin(element);
381-
writeln("module ", element.name, " {");
384+
writeln("namespace ", element.name, " {");
382385
++indent;
383386
innerClasses.forEach(function(inner) {
384387
handleClass(inner, element);

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
"serialization",
1919
"encoding"
2020
],
21-
"main": "src/index",
21+
"main": "index.js",
22+
"types": "index.d.ts",
2223
"bin": {
2324
"pbjs": "bin/pbjs",
2425
"pbts": "bin/pbts"
2526
},
26-
"types": "types/protobuf.js.d.ts",
2727
"scripts": {
2828
"bench": "node bench",
2929
"build": "gulp",
@@ -35,7 +35,7 @@
3535
"prepublish": "node scripts/prepublish",
3636
"prof": "node bench/prof",
3737
"test": "tape tests/*.js | tap-spec",
38-
"types": "node bin/pbts --name protobufjs --out types/protobuf.js.d.ts src && node node_modules/typescript/bin/tsc types/test.ts --lib es2015 --noEmit",
38+
"types": "node bin/pbts --name protobuf --out index.d.ts src && tsc tests/typescript.ts --lib es2015 --noEmit && tsc tests/data/test.ts --lib es2015 --noEmit",
3939
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
4040
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
4141
"make": "npm run lint && npm run test && npm run types && npm run build",
@@ -54,10 +54,10 @@
5454
"@protobufjs/utf8": "^1.0.5"
5555
},
5656
"optionalDependencies": {
57-
"long": "^3.2.0"
57+
"long": "^3.2.0",
58+
"@types/long": "^3.0.31"
5859
},
5960
"devDependencies": {
60-
"@types/long": "^3.0.31",
6161
"@types/node": "6.0.54",
6262
"benchmark": "^2.1.3",
6363
"browserify": "^13.1.1",

scripts/gentests.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var fs = require("fs"),
22
path = require("path"),
3-
pbjs = require("../cli/pbjs");
3+
pbjs = require("../cli/pbjs"),
4+
pbts = require("../cli/pbts");
45

56
[
67
"tests/data/package.proto",
@@ -20,6 +21,26 @@ var fs = require("fs"),
2021
], function(err) {
2122
if (err)
2223
throw err;
23-
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\/runtime\"/, "\"../../runtime\""), "utf8");
24+
var pathToRuntime = path.relative(path.dirname(out), "runtime").replace(/\\/g, "/");
25+
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\/runtime\"/g, JSON.stringify(pathToRuntime)), "utf8");
26+
console.log("pbjs: " + file + " -> " + out);
2427
})
2528
});
29+
30+
[
31+
"tests/data/test.js"
32+
]
33+
.forEach(function(file) {
34+
var out = file.replace(/\.js$/, ".d.ts");
35+
pbts.main([
36+
"--name", path.basename(out, ".d.ts"),
37+
"--out", out,
38+
file
39+
], function(err) {
40+
if (err)
41+
throw err;
42+
var pathToProtobufjs = path.relative(path.dirname(out), "").replace(/\\/g, "/");
43+
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\"/g, JSON.stringify(pathToProtobufjs)), "utf8");
44+
console.log("pbts: " + file + " -> " + out);
45+
});
46+
});

src/util/aspromise/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export = asPromise;
2+
13
/**
24
* Returns a promise from a node-style callback function.
35
* @memberof util

src/util/base64/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
export = base64;
2+
13
/**
24
* A minimal base64 implementation for number arrays.
35
* @memberof util
46
* @namespace
57
*/
6-
declare module base64 {
8+
declare namespace base64 {
79

810
/**
911
* Calculates the byte length of a base64 encoded string.

src/util/codegen/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export = codegen;
2+
13
/**
24
* A codegen instance as returned by {@link codegen}, that also is a sprintf-like appender function.
35
* @typedef Codegen

src/util/eventemitter/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export = EventEmitter;
2+
13
/**
24
* Constructs a new event emitter instance.
35
* @classdesc A minimal event emitter.

0 commit comments

Comments
 (0)