Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile Error with structs and useOptionals=all #578

Closed
cprovatas opened this issue May 31, 2022 · 2 comments · Fixed by #1008 · May be fixed by Graysonbarton/synthetics-sdk-nodejs#2
Closed

Compile Error with structs and useOptionals=all #578

cprovatas opened this issue May 31, 2022 · 2 comments · Fixed by #1008 · May be fixed by Graysonbarton/synthetics-sdk-nodejs#2
Labels

Comments

@cprovatas
Copy link

cprovatas commented May 31, 2022

Version 1.114.7
useOptionals=all

Doesn't seem to be properly handling struct fields. Either the generated struct code needs to add ? unwrapping logic or it should remove the optionality on the fields / values properties.

protoc --proto_path=./src/proto --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./src/proto/generated/ $(find ./src/proto/models -iname "*.proto") --ts_proto_opt=useOptionals=all --ts_proto_opt=outputPartialMethods=false --ts_proto_opt=unrecognizedEnum=false --ts_proto_opt=stringEnums=true --ts_proto_opt=useDate=false --ts_proto_opt=oneof=unions --ts_proto_opt=outputJsonMethods=false --ts_proto_opt=fileSuffix=.pb
        
[10:49:33] Finished 'protoc' after 1.25 s
[10:49:35] Starting 'dev'...
[10:49:35] Starting 'typescript'...
[10:49:35] src/proto/generated/google/protobuf/struct.pb.ts:134:9 - error TS2532: Object is possibly 'undefined'.

134         struct.fields[key] = object[key];
            ~~~~~~~~~~~~~

[10:49:35] src/proto/generated/google/protobuf/struct.pb.ts:142:17 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(o: {}): string[]', gave the following error.
    Argument of type '{ [key: string]: any; } | undefined' is not assignable to parameter of type '{}'.
      Type 'undefined' is not assignable to type '{}'.
  Overload 2 of 2, '(o: object): string[]', gave the following error.
    Argument of type '{ [key: string]: any; } | undefined' is not assignable to parameter of type 'object'.
      Type 'undefined' is not assignable to type 'object'.

142     Object.keys(message.fields).forEach((key) => {
                    ~~~~~~~~~~~~~~


[10:49:35] src/proto/generated/google/protobuf/struct.pb.ts:143:21 - error TS2532: Object is possibly 'undefined'.

143       object[key] = message.fields[key];
                        ~~~~~~~~~~~~~~

[10:49:35] src/proto/generated/google/protobuf/struct.pb.ts:358:5 - error TS2322: Type 'any[] | undefined' is not assignable to type 'any[]'.
  Type 'undefined' is not assignable to type 'any[]'.

358     return message.values;
        ~~~~~~~~~~~~~~~~~~~~~~
@cprovatas
Copy link
Author

I have a hack right now to remove the optional annotation, which is gross but works

const structPath = './src/proto/generated/google/protobuf/struct.pb.ts';
function fixGeneratedTypeScript(): void {
    const contents = fs.readFileSync(structPath, 'utf8');
    const result = contents
        .replace('fields?:', 'fields:')
        .replace('values?:', 'values:');

    fs.writeFileSync(structPath, result, 'utf8');
}

stephenh pushed a commit that referenced this issue Feb 28, 2024
Fixes #578 

Due to the use of the `.foreach` function TS has no context of the clue
that `struct.fields` is definently not undefined.
stephenh pushed a commit that referenced this issue Feb 28, 2024
## [1.167.9](v1.167.8...v1.167.9) (2024-02-28)

### Bug Fixes

* typescript errors for struct with optional=all ([#1008](#1008)) ([e838e38](e838e38)), closes [#578](#578)
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.167.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants