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

Prisma Client: wrong type when using spread operator to set default values on query args #19962

Closed
AntonyZ89 opened this issue Jun 25, 2023 · 3 comments · Fixed by #20180
Closed
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: client types Types in Prisma Client topic: clientExtensions topic: prisma-client topic: typescript
Milestone

Comments

@AntonyZ89
Copy link

Bug description

I'm using spread operator to set default values on query args of my findMany() function, but when I use it the generated type is incorrect.

How to reproduce

Repository class:

class ComponentRepository {
  /* ... */

  findAll<T extends Prisma.ComponentFindManyArgs>(
    params?: Prisma.SelectSubset<T, Prisma.ComponentFindManyArgs>,
  ) {
    return this.prisma.findMany({
      // default `include`
      include: { componentProp: true, componentStyle: true },
      ...params,
    });
  }

  /* ... */
}

If I pass only params as args of findMany the type of component.componentStyle (see below) is correct.

class ComponentRepository {
  /* ... */

  findAll<T extends Prisma.ComponentFindManyArgs>(
    params?: Prisma.SelectSubset<T, Prisma.ComponentFindManyArgs>,
  ) {
    return this.prisma.findMany(params);
  }

  /* ... */
}

Expected behavior

componentStyle need exists in component.componentStyle.

Prisma information

const componentWithImages = await this.componentRepository.findAll({
  where: {
    overlayId,
    id: {
      notIn: params.map(({ id }) => id),
    },
    componentStyle: {
      some: { key: 'backgroundImage' },
    },
  },
  // override include
  include: { componentStyle: true },
});

for (const component of componentWithImages) {
  /**
    * error TS2339: Property 'componentStyle' does not exist on type '({ componentProp: (GetResult<{ componentId: string; key: string; value: string; variableAssign: string | null; type: ComponentPropType; }, unknown> & {})[]; componentStyle: (GetResult<...> & {})[]; } | {} | { ...; } | undefined) & GetResult<...> & {}'.
    */
  const style = component.componentStyle.find(
    (s) => s.key === 'backgroundImage',
  );

  /* ... */
}

Environment & setup

  • OS: WSL 2 Ubuntu 22.04.1 LTS
  • Database: PostgreSQL
  • Node.js version: v18.14.0

Prisma Version

prisma                  : 4.16.1
@prisma/client          : 4.16.1
Current platform        : debian-openssl-3.0.x
Query Engine (Node-API) : libquery-engine b20ead4d3ab9e78ac112966e242ded703f4a052c (at ../../node_modules/.pnpm/@prisma+engines@4.16.1/node_modules/@prisma/engines/libquery_engine-debian-openssl-3.0.x.so.node)
Migration Engine        : migration-engine-cli b20ead4d3ab9e78ac112966e242ded703f4a052c (at ../../node_modules/.pnpm/@prisma+engines@4.16.1/node_modules/@prisma/engines/migration-engine-debian-openssl-3.0.x)
Format Wasm             : @prisma/prisma-fmt-wasm 4.16.0-66.b20ead4d3ab9e78ac112966e242ded703f4a052c
Default Engines Hash    : b20ead4d3ab9e78ac112966e242ded703f4a052c
Studio                  : 0.484.0
@AntonyZ89 AntonyZ89 added the kind/bug A reported bug. label Jun 25, 2023
@jkomyno jkomyno added tech/typescript Issue for tech TypeScript. team/client Issue for team Client. topic: typescript labels Jun 26, 2023
@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: prisma-client topic: client types Types in Prisma Client labels Jun 26, 2023
@Jolg42 Jolg42 changed the title Wrong type when using spread operator Prisma Client: wrong type when using spread operator Jun 26, 2023
@Jolg42 Jolg42 changed the title Prisma Client: wrong type when using spread operator Prisma Client: wrong type when using spread operator to set default values on query args Jun 26, 2023
@Jolg42 Jolg42 self-assigned this Jun 26, 2023
@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jun 26, 2023
@Jolg42
Copy link
Member

Jolg42 commented Jun 26, 2023

I can reproduce on 4.15.0/4.16.1/4.17.0-dev.38(latest internal dev version)

@dkelsen
Copy link

dkelsen commented Nov 19, 2023

This still appears to be an issue with conditional spreads.

// Missing `venue` property in type.

const events = await prisma.event.findMany({
        take: first,
        ...(cursor && {
          skip: 1,
          cursor: {
            id: cursor,
          },
        }),
        include: {
          venue: true,
        },
      }))

@Jolg42
Copy link
Member

Jolg42 commented Nov 20, 2023

@dkelsen This is issue is closed, could you open a new issue? We would need more information to reproduce and confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: client types Types in Prisma Client topic: clientExtensions topic: prisma-client topic: typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants