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

bug: inference error when creating OpenApiDocument. The inferred type of 'openApiDocument' cannot be named without a reference to '.pnpm/[email protected]/node_modules/openapi-types'. This is likely not portable. A type annotation is necessary.ts (2742) #408

Open
MiniSuperDev opened this issue Oct 10, 2023 · 8 comments

Comments

@MiniSuperDev
Copy link

image

The inferred type of 'openApiDocument' cannot be named without a reference to '.pnpm/[email protected]/node_modules/openapi-types'. This is likely not portable. A type annotation is necessary.ts(2742)

You can clone this repo in the linked commit:
https://github.com/MiniSuperDev/turbo-trpc-bug/tree/93a6efbce0a9c3a178f411831444ee52104a0c78

The error is in this line:
https://github.com/MiniSuperDev/turbo-trpc-bug/blob/93a6efbce0a9c3a178f411831444ee52104a0c78/apps/express-api/src/openapi.ts#L4

I'm using a monorepo with turborepo and pnpm.

Thanks

@MaximilianGaedig
Copy link

I have a similar problem, when I try to build my api library form another package using tsup I get the error: ../../node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/src/core/initTRPC.ts(64,3): error TS2742: The inferred type of 'create' cannot be named without a reference to '.pnpm/@[email protected]/node_modules/@trpc/server/src'. This is likely not portable. A type annotation is necessary.

When commenting the openapi meta line:

const t = initTRPC
  // .meta<OpenApiMeta>() // With this line commented the code compiles
  .context<typeof createTRPCContext>().create({
    transformer: superjson,
    errorFormatter({ shape, error }) {
      return {
        ...shape,
        data: {
          ...shape.data,
          zodError:
            error.cause instanceof ZodError ? error.cause.flatten() : null,
        },
      };
    },
  });

@IsaiahByDayah
Copy link

@MaximilianGaedig Have you found any workaround for this? Im running into the same issue trying to add trpc-openapi to my api package in a pnpm+turborepo codebase

@MaximilianGaedig
Copy link

MaximilianGaedig commented Feb 11, 2024

Hey @IsaiahByDayah, yea, we're using it in production for a while already, we've just casted it to OpenApiRouter

import { type OpenApiRouter, generateOpenApiDocument } from 'trpc-openapi';

const openApiDocument = generateOpenApiDocument((openApiRouter as OpenApiRouter),{...})

@IsaiahByDayah
Copy link

Hmmm, @MaximilianGaedig this fixed your issue when adding .meta<OpenApiMeta>() to the initTRPC chain? Was there any other exports you had to do to get that piece working? I never actually ran into any issues with generateOpenApiDocument, but even just adding the meta call throws the "The inferred type of 'create' cannot be named ..." error when I try to build

@IsaiahByDayah
Copy link

Would you be able to share a link to your codebase? Happy to try and dig through the source myself to figure out if theres anything different between setups

@MaximilianGaedig
Copy link

MaximilianGaedig commented Feb 11, 2024

Hmmm, @MaximilianGaedig this fixed your issue when adding .meta<OpenApiMeta>() to the initTRPC chain? Was there any other exports you had to do to get that piece working? I never actually ran into any issues with generateOpenApiDocument, but even just adding the meta call throws the "The inferred type of 'create' cannot be named ..." error when I try to build

I just left that commented, about the meta calls, I have not had the same happen

Would you be able to share a link to your codebase? Happy to try and dig through the source myself to figure out if theres anything different between setups

Can't do that, sorry, it's a proprietary product

a part of my tsconfig.json which might help:

{
  "compilerOptions": {
    "incremental": false,
    "module": "node16",
    "moduleResolution": "node16"
  },
  "include": [
    "src/index.ts"
  ],
  "exclude": [
    "node_modules",
    "@trpc/server"
  ]
}

@IsaiahByDayah
Copy link

Oh, strange 🤔. And you guys are able to still get type inference when you set the meta value for procedures? ie:

export const appRouter = t.router({
  sayHello: t.procedure
    .meta({ openapi: { method: 'GET', path: '/say-hello' } }) // 👈 this line here
    .input(z.object({ name: z.string() }))
    .output(z.object({ greeting: z.string() }))
    .query(({ input }) => {
      return { greeting: `Hello ${input.name}!` };
    });
});

@MaximilianGaedig
Copy link

Oh, strange 🤔. And you guys are able to still get type inference when you set the meta value for procedures? ie:

export const appRouter = t.router({

  sayHello: t.procedure

    .meta({ openapi: { method: 'GET', path: '/say-hello' } }) // 👈 this line here

    .input(z.object({ name: z.string() }))

    .output(z.object({ greeting: z.string() }))

    .query(({ input }) => {

      return { greeting: `Hello ${input.name}!` };

    });

});

we might be not using a part of this, well we have those meta tags for sure, but we don't use the types from that directly, we generate this openapi file and then use it in another package where we have an openapi-fetch client from that

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

No branches or pull requests

3 participants