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

TS2742: The inferred type of 'X' cannot be named without a reference #60624

Closed
Aliaksandr-Kasko-JazzTeam opened this issue Nov 27, 2024 · 7 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@Aliaksandr-Kasko-JazzTeam
Copy link

Aliaksandr-Kasko-JazzTeam commented Nov 27, 2024

πŸ”Ž Search Terms

ts2742, inferred type, bufbuild, protobuf, es, proto3

πŸ•— Version & Regression Information

  • This changed between versions 5.6.3 and 5.7.x

⏯ Playground Link

No response

πŸ’» Code

πŸ™ Actual behavior

When using proto files with enums generated with @bufbuild/protoc-gen-es we've got this error on project build with typescript 5.7.x.

error TS2742: The inferred type of 'Price' cannot be named without a reference to '../../../../node_modules/@bufbuild/protobuf/dist/esm/private/enum.js'. This is likely not portable. A type annotation is necessary.

58 export const Price = /*@__PURE__*/ proto3.makeEnum(
                ~~~~~

πŸ™‚ Expected behavior

Project builds successfull with TypeScript <= 5.6.3.

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Project builds successfull with TypeScript <= 5.6.3.

What's the emitted declaration for Price there?

@Aliaksandr-Kasko-JazzTeam
Copy link
Author

@Andarist its enum generated from proto file

...
enum Price {
  PRICE_UNSPECIFIED = 0;
  PRICE_PAID = 1;
  PRICE_FREE = 2;
}
...

@Andarist
Copy link
Contributor

This error is raised on:

export const Price = /*@__PURE__*/ proto3.makeEnum(

when attempting to generate a .d.ts for the containing file. I wonder what TS put before at this position:

// generated.d.ts
export declare const Price: ???

@Aliaksandr-Kasko-JazzTeam
Copy link
Author

in d.ts file generated by @bufbuild/protoc-gen-es
buf.gen.yaml

version: v1
plugins:
  - plugin: es
    out: src/protos
    opt: target=js+dts
  - plugin: connect-es
    out: src/protos
    opt:
      - target=js+dts

I see next lines

export declare enum Price {
  /**
   * @generated from enum value: PRICE_UNSPECIFIED = 0;
   */
  UNSPECIFIED = 0,

  /**
   * @generated from enum value: PRICE_PAID = 1;
   */
  PAID = 1,

  /**
   * @generated from enum value: PRICE_FREE = 2;
   */
  FREE = 2,
}

when I build project with tsc (typescript 5.6.3) then in dist folder d.ts proto file looks like:

export const Price: import("../../../../node_modules/@bufbuild/protobuf/dist/esm/private/enum.js").EnumObject;

I don't know what else you want from me.

@Andarist
Copy link
Contributor

I was interested exactly in this information:

export const Price: import("../../../../node_modules/@bufbuild/protobuf/dist/esm/private/enum.js").EnumObject;

Thank you for providing it.

To be fair, this emitted declaration wasn't really that portable in the first place. I don't know what changed in which PR but an emitted type like this is sus.

To address this issue I'd recommend doing this:

  • adding a dependency on @bufbuild/protobuf. Presumably this is a dependency of your dependency (a so-called transitive dependency)
  • making sure that EnumObject can be imported from @bufbuild/protobuf
  • try to annotate const Price: ? manually. If you can't do it manually then the compiler won't be able to do so either

@RyanCavanaugh
Copy link
Member

See #58176 (comment) and #42873 (comment)

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Nov 27, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants