Skip to content

Commit

Permalink
fix: Compilation error for nested repeated fields with useOptionals=a…
Browse files Browse the repository at this point in the history
…ll (#1112)

The TS code generated for the following protobuf code with the `useOptionals=all` parameter does not compile.

```proto
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message NestedList { repeated google.protobuf.StringValue a_string = 1; }

message Example { NestedList list = 1; }
```

The problem lies in the `decode` function not taking into account that the `value` after decoding could also be `undefined`. Therefore the following generated line:

```ts
message.aString!.push(StringValue.decode(reader, reader.uint32()).value);
```

results in the following TS compilation error:

```
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.ts(2345)
```

The fix is to push the decoded element to the array only if it's not an `undefined` value.
  • Loading branch information
vecerek committed Sep 29, 2024
1 parent ffacf3a commit 08981bf
Show file tree
Hide file tree
Showing 223 changed files with 5,168 additions and 2,140 deletions.
3 changes: 2 additions & 1 deletion integration/angular/simple-message.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion integration/async-iterable-services-abort-signal/simple.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion integration/async-iterable-services/simple.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions integration/avoid-import-conflicts-folder-name/ui.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions integration/avoid-import-conflicts-folder-name/ui/simple.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions integration/avoid-import-conflicts/simple.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions integration/avoid-import-conflicts/simple2.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions integration/barrel-imports/bar.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions integration/barrel-imports/foo.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08981bf

Please sign in to comment.