Skip to content

Commit 1405d4b

Browse files
feat: Ensure strict(er) TS compliance for the generated code (#868)
* Ensure strict(er) TS compliance for the generated code - Add a new `tsc:check` script to ensure that there are no TS files with undetected errors. - Split the TS configs in `integrations` so the compiled proto TS files can underly much stricter configuration requirements than the test files themselves. - Add `DOM` to `compilerOptions.lib` for integration tests to have DOM's `AbortSignal` in scope. - Apply the strictest TS preset there is to the compiled proto TS files (with a few exceptions that may be resolved later). - Update the generated code to adhere to the stricter TS config. - Modernise a few areas of the generated code. - Add `tsc:check` to the CI script * fixup! Ensure strict(er) TS compliance for the generated code * fixup! Ensure strict(er) TS compliance for the generated code * fixup! Ensure strict(er) TS compliance for the generated code * fixup! Ensure strict(er) TS compliance for the generated code * fixup! Ensure strict(er) TS compliance for the generated code
1 parent 5ec618c commit 1405d4b

File tree

99 files changed

+674
-618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+674
-618
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [14.x, 16.x, 18.x]
15+
node-version: [16.x, 18.x, 20.x]
1616

1717
steps:
1818
- uses: actions/checkout@v3

.github/workflows/lint.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [14.x]
15+
node-version: [20.x]
1616

1717
steps:
1818
- uses: actions/checkout@v3
@@ -21,7 +21,16 @@ jobs:
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323
- name: install dependencies
24-
run: yarn install || echo "ignore failure"
24+
run: yarn install
25+
- name: Prepare Integration
26+
run: ./integration/pbjs.sh
27+
# This will fail if any git-tracked file has changed
28+
- name: Codegen
29+
run: yarn bin2ts
30+
- name: TypeScript
31+
run: yarn tsc:check
32+
env:
33+
CI: true
2534
- name: Prettier
2635
run: yarn format:check
2736
env:

integration/batching-with-context-esModuleInterop/batching.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ export class EntityServiceClientImpl<Context extends DataLoaders> implements Ent
705705
return new DataLoader<string, Entity>((ids) => {
706706
const request = { ids };
707707
return this.BatchMapQuery(ctx, request).then((res) => {
708-
return ids.map((key) => res.entities[key]);
708+
return ids.map((key) => res.entities[key] ?? fail());
709709
});
710710
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
711711
});
@@ -770,3 +770,7 @@ function isObject(value: any): boolean {
770770
function isSet(value: any): boolean {
771771
return value !== null && value !== undefined;
772772
}
773+
774+
function fail(message?: string): never {
775+
throw new Error(message ?? "Failed");
776+
}

integration/batching-with-context/batching.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ export class EntityServiceClientImpl<Context extends DataLoaders> implements Ent
705705
return new DataLoader<string, Entity>((ids) => {
706706
const request = { ids };
707707
return this.BatchMapQuery(ctx, request).then((res) => {
708-
return ids.map((key) => res.entities[key]);
708+
return ids.map((key) => res.entities[key] ?? fail());
709709
});
710710
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
711711
});
@@ -770,3 +770,7 @@ function isObject(value: any): boolean {
770770
function isSet(value: any): boolean {
771771
return value !== null && value !== undefined;
772772
}
773+
774+
function fail(message?: string): never {
775+
throw new Error(message ?? "Failed");
776+
}

integration/bytes-as-base64/message.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export const Message = {
3333
},
3434
};
3535

36-
declare var self: any | undefined;
37-
declare var window: any | undefined;
38-
declare var global: any | undefined;
39-
var tsProtoGlobalThis: any = (() => {
36+
declare const self: any | undefined;
37+
declare const window: any | undefined;
38+
declare const global: any | undefined;
39+
const tsProtoGlobalThis: any = (() => {
4040
if (typeof globalThis !== "undefined") {
4141
return globalThis;
4242
}

integration/bytes-node/google/protobuf/wrappers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,10 @@ export const BytesValue = {
599599
},
600600
};
601601

602-
declare var self: any | undefined;
603-
declare var window: any | undefined;
604-
declare var global: any | undefined;
605-
var tsProtoGlobalThis: any = (() => {
602+
declare const self: any | undefined;
603+
declare const window: any | undefined;
604+
declare const global: any | undefined;
605+
const tsProtoGlobalThis: any = (() => {
606606
if (typeof globalThis !== "undefined") {
607607
return globalThis;
608608
}

integration/bytes-node/point.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ export const Point = {
8181
},
8282
};
8383

84-
declare var self: any | undefined;
85-
declare var window: any | undefined;
86-
declare var global: any | undefined;
87-
var tsProtoGlobalThis: any = (() => {
84+
declare const self: any | undefined;
85+
declare const window: any | undefined;
86+
declare const global: any | undefined;
87+
const tsProtoGlobalThis: any = (() => {
8888
if (typeof globalThis !== "undefined") {
8989
return globalThis;
9090
}

integration/extensions/test.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ export function enumToJSON(object: Enum): string {
4444
}
4545

4646
export interface Extendable {
47-
field?: string;
48-
_unknownFields?: { [key: number]: Uint8Array[] };
47+
field?: string | undefined;
48+
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
4949
}
5050

5151
export interface Nested {
52-
field?: string;
53-
_unknownFields?: { [key: number]: Uint8Array[] };
52+
field?: string | undefined;
53+
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
5454
}
5555

5656
export interface Group {
57-
name?: string;
58-
value?: string;
59-
_unknownFields?: { [key: number]: Uint8Array[] };
57+
name?: string | undefined;
58+
value?: string | undefined;
59+
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
6060
}
6161

6262
function createBaseExtendable(): Extendable {
@@ -69,8 +69,7 @@ export const Extendable = {
6969
writer.uint32(10).string(message.field);
7070
}
7171
if (message._unknownFields !== undefined) {
72-
for (const key in message._unknownFields) {
73-
const values = message._unknownFields[key];
72+
for (const [key, values] of Object.entries(message._unknownFields)) {
7473
const tag = parseInt(key, 10);
7574
for (const value of values) {
7675
writer.uint32(tag);
@@ -216,7 +215,7 @@ export const Nested = {
216215
},
217216
decode: (tag: number, input: Uint8Array[]): Nested[] => {
218217
const values: Nested[] = [];
219-
for (var buffer of input) {
218+
for (const buffer of input) {
220219
const reader = _m0.Reader.create(buffer);
221220
values.push(Nested.decode(reader, reader.uint32()));
222221
}
@@ -230,8 +229,7 @@ export const Nested = {
230229
writer.uint32(10).string(message.field);
231230
}
232231
if (message._unknownFields !== undefined) {
233-
for (const key in message._unknownFields) {
234-
const values = message._unknownFields[key];
232+
for (const [key, values] of Object.entries(message._unknownFields)) {
235233
const tag = parseInt(key, 10);
236234
for (const value of values) {
237235
writer.uint32(tag);
@@ -317,8 +315,7 @@ export const Group = {
317315
writer.uint32(18).string(message.value);
318316
}
319317
if (message._unknownFields !== undefined) {
320-
for (const key in message._unknownFields) {
321-
const values = message._unknownFields[key];
318+
for (const [key, values] of Object.entries(message._unknownFields)) {
322319
const tag = parseInt(key, 10);
323320
for (const value of values) {
324321
writer.uint32(tag);
@@ -422,7 +419,7 @@ export const packed: Extension<number[]> = {
422419
},
423420
decode: (tag: number, input: Uint8Array[]): number[] => {
424421
const values: number[] = [];
425-
for (var buffer of input) {
422+
for (const buffer of input) {
426423
const reader = _m0.Reader.create(buffer);
427424
if (tag == 42) {
428425
const end2 = reader.uint32() + reader.pos;
@@ -457,7 +454,7 @@ export const repeated: Extension<number[]> = {
457454
},
458455
decode: (tag: number, input: Uint8Array[]): number[] => {
459456
const values: number[] = [];
460-
for (var buffer of input) {
457+
for (const buffer of input) {
461458
const reader = _m0.Reader.create(buffer);
462459
if (tag == 50) {
463460
const end2 = reader.uint32() + reader.pos;
@@ -488,7 +485,7 @@ export const bytes: Extension<Uint8Array> = {
488485
return encoded;
489486
},
490487
decode: (tag: number, input: Uint8Array[]): Uint8Array => {
491-
const reader = _m0.Reader.create(input[input.length - 1]);
488+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
492489
return reader.bytes();
493490
},
494491
};
@@ -508,7 +505,7 @@ export const string: Extension<string> = {
508505
return encoded;
509506
},
510507
decode: (tag: number, input: Uint8Array[]): string => {
511-
const reader = _m0.Reader.create(input[input.length - 1]);
508+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
512509
return reader.string();
513510
},
514511
};
@@ -528,7 +525,7 @@ export const long: Extension<Long> = {
528525
return encoded;
529526
},
530527
decode: (tag: number, input: Uint8Array[]): Long => {
531-
const reader = _m0.Reader.create(input[input.length - 1]);
528+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
532529
return reader.int64() as Long;
533530
},
534531
};
@@ -548,7 +545,7 @@ export const fixed: Extension<Long> = {
548545
return encoded;
549546
},
550547
decode: (tag: number, input: Uint8Array[]): Long => {
551-
const reader = _m0.Reader.create(input[input.length - 1]);
548+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
552549
return reader.fixed64() as Long;
553550
},
554551
};
@@ -568,7 +565,7 @@ export const enumField: Extension<Enum> = {
568565
return encoded;
569566
},
570567
decode: (tag: number, input: Uint8Array[]): Enum => {
571-
const reader = _m0.Reader.create(input[input.length - 1]);
568+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
572569
return reader.int32() as any;
573570
},
574571
};
@@ -586,7 +583,7 @@ export const group: Extension<Group> = {
586583
return encoded;
587584
},
588585
decode: (tag: number, input: Uint8Array[]): Group => {
589-
const reader = _m0.Reader.create(input[input.length - 1]);
586+
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
590587
return Group.decode(reader);
591588
},
592589
};
@@ -623,3 +620,7 @@ export interface Extension<T> {
623620
repeated: boolean;
624621
packed: boolean;
625622
}
623+
624+
function fail(message?: string): never {
625+
throw new Error(message ?? "Failed");
626+
}

integration/file-suffix/google/protobuf/timestamp.pb.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ export const Timestamp = {
182182
},
183183
};
184184

185-
declare var self: any | undefined;
186-
declare var window: any | undefined;
187-
declare var global: any | undefined;
188-
var tsProtoGlobalThis: any = (() => {
185+
declare const self: any | undefined;
186+
declare const window: any | undefined;
187+
declare const global: any | undefined;
188+
const tsProtoGlobalThis: any = (() => {
189189
if (typeof globalThis !== "undefined") {
190190
return globalThis;
191191
}

integration/from-partial-no-initialize/test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import * as _m0 from "protobufjs/minimal";
44
export const protobufPackage = "";
55

66
export interface TPartialMessage {
7-
field?: string;
7+
field?: string | undefined;
88
}
99

1010
export interface TPartial {
11-
number?: number;
12-
string?: string;
13-
map?: { [key: string]: string };
14-
message?: TPartialMessage;
15-
repeatedMessage?: TPartialMessage[];
16-
repeatedString?: string[];
17-
repeatedNumber?: number[];
11+
number?: number | undefined;
12+
string?: string | undefined;
13+
map?: { [key: string]: string } | undefined;
14+
message?: TPartialMessage | undefined;
15+
repeatedMessage?: TPartialMessage[] | undefined;
16+
repeatedString?: string[] | undefined;
17+
repeatedNumber?: number[] | undefined;
1818
}
1919

2020
export interface TPartial_MapEntry {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Foo {
2+
foo: "bar";
3+
}

0 commit comments

Comments
 (0)