Skip to content

Commit ff85800

Browse files
committed
Breaking: Replaced Buffer and Long types with interfaces and removed stubs; CLI: Further TypeScript definition improvements
1 parent 22f907c commit ff85800

File tree

10 files changed

+103
-212
lines changed

10 files changed

+103
-212
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,7 @@ Note that the service API is meant for clients. Implementing a server-side endpo
465465

466466
The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) will automatically detect and use them for code completion.
467467

468-
**Note** that the npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`. If you are not building for node and/or not using long.js and want to exclude their full type definitions manually for whatever reason, there are two stubs available that can be referenced instead of the respective full type definition:
469-
470-
```ts
471-
/// <reference path="./node_modules/protobufjs/stub-long.d.ts" />
472-
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
473-
```
468+
The npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`. If you are not building for node and/or not using long.js, it should be safe to exclude them manually.
474469

475470
#### Using the JS API
476471

cli/lib/tsd-jsdoc/publish.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function writeln() {
134134
indentWritten = false;
135135
}
136136

137-
var skipTagsRe = /@(type|memberof|name|exports|interface|extends|implements|constructor|function|template|property|this|typedef|variation|example|returns \{undefined})[^@$]*/g,
137+
var skipTagsRe = /@(type|tstype|const|memberof|name|exports|interface|extends|implements|constructor|function|template|property|this|typedef|variation|example|returns \{undefined})[^@$]*/g,
138138
skipTypeRe = /(@[^ ]+) \{[^\s$]+}/g;
139139

140140
// writes a comment
@@ -468,7 +468,7 @@ function handleClass(element, parent) {
468468
// class-compatible members
469469
var incompatible = [];
470470
getChildrenOf(element).forEach(function(child) {
471-
if (isClassLike(child) || element.kind === "module" || element.kind === "typedef") {
471+
if (isClassLike(child) || child.kind === "module" || child.kind === "typedef" || child.isEnum) {
472472
incompatible.push(child);
473473
return;
474474
}
@@ -515,7 +515,8 @@ function handleMember(element, parent) {
515515

516516
} else {
517517

518-
if (isClassLike(parent)) {
518+
var inClass = isClassLike(parent);
519+
if (inClass) {
519520
write(element.access || "public", " ");
520521
if (element.scope === "static")
521522
write("static ");

ext/descriptor/index.d.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export interface IFieldDescriptorProto {
7575
defaultValue?: string;
7676
oneofIndex?: number;
7777
jsonName?: any;
78-
options?: IFieldOptionsProperties;
78+
options?: IFieldOptions;
7979
}
8080

8181
type IFieldDescriptorProtoLabel = number;
@@ -138,17 +138,9 @@ export const FileDescriptorSet: $protobuf.Type;
138138

139139
export const FileDescriptorProto: $protobuf.Type;
140140

141-
export const DescriptorProto: $protobuf.Type;
141+
export const DescriptorProto: $protobuf.Type & { ExtensionRange: $protobuf.Type, ReservedRange: $protobuf.Type};
142142

143-
export const DescriptorProtoExtensionRange: $protobuf.Type;
144-
145-
export const DescriptorProtoReservedRange: $protobuf.Type;
146-
147-
export const FieldDescriptorProto: $protobuf.Type;
148-
149-
export const FieldDescriptorProtoLabel: $protobuf.Enum;
150-
151-
export const FieldDescriptorProtoType: $protobuf.Enum;
143+
export const FieldDescriptorProto: $protobuf.Type & { Label: $protobuf.Enum, Type: $protobuf.Enum};
152144

153145
export const OneofDescriptorProto: $protobuf.Type;
154146

@@ -160,17 +152,11 @@ export const EnumValueDescriptorProto: $protobuf.Type;
160152

161153
export const MethodDescriptorProto: $protobuf.Type;
162154

163-
export const FileOptions: $protobuf.Type;
164-
165-
export const FileOptionsOptimizeMode: $protobuf.Enum;
155+
export const FileOptions: $protobuf.Type & { OptimizeMode: $protobuf.Enum};
166156

167157
export const MessageOptions: $protobuf.Type;
168158

169-
export const FieldOptions: $protobuf.Type;
170-
171-
export const FieldOptionsCType: $protobuf.Enum;
172-
173-
export const FieldOptionsJSType: $protobuf.Enum;
159+
export const FieldOptions: $protobuf.Type & { CType: $protobuf.Enum, JSType: $protobuf.Enum};
174160

175161
export const OneofOptions: $protobuf.Type;
176162

@@ -182,14 +168,8 @@ export const ServiceOptions: $protobuf.Type;
182168

183169
export const MethodOptions: $protobuf.Type;
184170

185-
export const UninterpretedOption: $protobuf.Type;
186-
187-
export const UninterpretedOptionNamePart: $protobuf.Type;
188-
189-
export const SourceCodeInfo: $protobuf.Type;
190-
191-
export const SourceCodeInfoLocation: $protobuf.Type;
171+
export const UninterpretedOption: $protobuf.Type & { NamePart: $protobuf.Type};
192172

193-
export const GeneratedCodeInfo: $protobuf.Type;
173+
export const SourceCodeInfo: $protobuf.Type & { Location: $protobuf.Type};
194174

195-
export const GeneratedCodeInfoAnnotation: $protobuf.Type;
175+
export const GeneratedCodeInfo: $protobuf.Type & { Annotation: $protobuf.Type};

ext/descriptor/index.js

Lines changed: 35 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) {
313313
* @property {string} [defaultValue] Literal default value
314314
* @property {number} [oneofIndex] Oneof index if part of a oneof
315315
* @property {*} [jsonName] Not supported
316-
* @property {IFieldOptionsProperties} [options] Field options
316+
* @property {IFieldOptions} [options] Field options
317317
*/
318318

319319
/**
@@ -874,54 +874,28 @@ function underScore(str) {
874874
* Reflected descriptor proto.
875875
* @name DescriptorProto
876876
* @type {Type}
877+
* @property {Type} ExtensionRange
878+
* @property {Type} ReservedRange
877879
* @const
878-
* @tstype $protobuf.Type
879-
*/
880-
881-
/**
882-
* Reflected descriptor proto extension range.
883-
* @name DescriptorProtoExtensionRange
884-
* @type {Type}
885-
* @const
886-
* @tstype $protobuf.Type
880+
* @tstype $protobuf.Type & {
881+
* ExtensionRange: $protobuf.Type,
882+
* ReservedRange: $protobuf.Type
883+
* }
887884
*/
888-
exports.DescriptorProtoExtensionRange = exports.DescriptorProto.ExtensionRange;
889-
890-
/**
891-
* Reflected descriptor proto reserved range.
892-
* @name DescriptorProtoReservedRange
893-
* @type {Type}
894-
* @const
895-
* @tstype $protobuf.Type
896-
*/
897-
exports.DescriptorProtoReservedRange = exports.DescriptorProto.ReservedRange;
898885

899886
/**
900887
* Reflected field descriptor proto.
901888
* @name FieldDescriptorProto
902889
* @type {Type}
890+
* @property {Enum} Label
891+
* @property {Enum} Type
903892
* @const
904-
* @tstype $protobuf.Type
893+
* @tstype $protobuf.Type & {
894+
* Label: $protobuf.Enum,
895+
* Type: $protobuf.Enum
896+
* }
905897
*/
906898

907-
/**
908-
* Reflected field descriptor proto label.
909-
* @name FieldDescriptorProtoLabel
910-
* @type {Enum}
911-
* @const
912-
* @tstype $protobuf.Enum
913-
*/
914-
exports.FieldDescriptorProtoLabel = exports.FieldDescriptorProto.Label;
915-
916-
/**
917-
* Reflected field descriptor proto type.
918-
* @name FieldDescriptorProtoType
919-
* @type {Enum}
920-
* @const
921-
* @tstype $protobuf.Enum
922-
*/
923-
exports.FieldDescriptorProtoType = exports.FieldDescriptorProto.Type;
924-
925899
/**
926900
* Reflected oneof descriptor proto.
927901
* @name OneofDescriptorProto
@@ -966,19 +940,13 @@ exports.FieldDescriptorProtoType = exports.FieldDescriptorProto.Type;
966940
* Reflected file options.
967941
* @name FileOptions
968942
* @type {Type}
943+
* @property {Enum} OptimizeMode
969944
* @const
970-
* @tstype $protobuf.Type
945+
* @tstype $protobuf.Type & {
946+
* OptimizeMode: $protobuf.Enum
947+
* }
971948
*/
972949

973-
/**
974-
* Reflected file options optimize mode.
975-
* @name FileOptionsOptimizeMode
976-
* @type {Type}
977-
* @const
978-
* @tstype $protobuf.Enum
979-
*/
980-
exports.FileOptionsOptimizeMode = exports.FileOptions.OptimizeMode;
981-
982950
/**
983951
* Reflected message options.
984952
* @name MessageOptions
@@ -991,27 +959,14 @@ exports.FileOptionsOptimizeMode = exports.FileOptions.OptimizeMode;
991959
* Reflected field options.
992960
* @name FieldOptions
993961
* @type {Type}
962+
* @property {Enum} CType
963+
* @property {Enum} JSType
994964
* @const
995-
* @tstype $protobuf.Type
996-
*/
997-
998-
/**
999-
* Reflected field options c-type.
1000-
* @name FieldOptionsCType
1001-
* @type {Enum}
1002-
* @const
1003-
* @tstype $protobuf.Enum
1004-
*/
1005-
exports.FieldOptionsCType = exports.FieldOptions.CType;
1006-
1007-
/**
1008-
* Reflected field options js-type.
1009-
* @name FieldOptionsJSType
1010-
* @type {Enum}
1011-
* @const
1012-
* @tstype $protobuf.Enum
965+
* @tstype $protobuf.Type & {
966+
* CType: $protobuf.Enum,
967+
* JSType: $protobuf.Enum
968+
* }
1013969
*/
1014-
exports.FieldOptionsJSType = exports.FieldOptions.JSType;
1015970

1016971
/**
1017972
* Reflected oneof options.
@@ -1057,49 +1012,31 @@ exports.FieldOptionsJSType = exports.FieldOptions.JSType;
10571012
* Reflected uninterpretet option.
10581013
* @name UninterpretedOption
10591014
* @type {Type}
1015+
* @property {Type} NamePart
10601016
* @const
1061-
* @tstype $protobuf.Type
1062-
*/
1063-
1064-
/**
1065-
* Reflected uninterpreted option name part.
1066-
* @name UninterpretedOptionNamePart
1067-
* @type {Type}
1068-
* @const
1069-
* @tstype $protobuf.Type
1017+
* @tstype $protobuf.Type & {
1018+
* NamePart: $protobuf.Type
1019+
* }
10701020
*/
1071-
exports.UninterpretedOptionNamePart = exports.UninterpretedOption.NamePart;
10721021

10731022
/**
10741023
* Reflected source code info.
10751024
* @name SourceCodeInfo
10761025
* @type {Type}
1026+
* @property {Type} Location
10771027
* @const
1078-
* @tstype $protobuf.Type
1079-
*/
1080-
1081-
/**
1082-
* Reflected source code info location.
1083-
* @name SourceCodeInfoLocation
1084-
* @type {Type}
1085-
* @const
1086-
* @tstype $protobuf.Type
1028+
* @tstype $protobuf.Type & {
1029+
* Location: $protobuf.Type
1030+
* }
10871031
*/
1088-
exports.SourceCodeInfoLocation = exports.SourceCodeInfo.Location;
10891032

10901033
/**
10911034
* Reflected generated code info.
10921035
* @name GeneratedCodeInfo
10931036
* @type {Type}
1037+
* @property {Type} Annotation
10941038
* @const
1095-
* @tstype $protobuf.Type
1096-
*/
1097-
1098-
/**
1099-
* Reflected generated code info annotation.
1100-
* @name GeneratedCodeInfoAnnotation
1101-
* @type {Type}
1102-
* @const
1103-
* @tstype $protobuf.Type
1039+
* @tstype $protobuf.Type & {
1040+
* Annotation: $protobuf.Type
1041+
* }
11041042
*/
1105-
exports.GeneratedCodeInfoAnnotation = exports.GeneratedCodeInfo.Annotation;

0 commit comments

Comments
 (0)