Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Getting Type '(TypeWithID & Record<string, unknown>)[]' from local payload api

default discord avatar
normanprice1last year
21

Im getting '(TypeWithID & Record<string, unknown>)[]' from local payload api instead of one from generated types.



// app/(app)/page.tsx

...
import { getPayload } from 'payload';
import configPromise from '@payload-config';
import { type News } from '~~/payload-types';

const getArticles = async () => {
  const payload = await getPayload({
    config: configPromise,
  });

  const newsCollection: News[] = await payload
    .find({
      collection: 'news',
      sort: '-createdAt',
    })
    .then((res) => res.docs.filter((doc) => doc._status === 'published'));

  return newsCollection;
};

...

How can i get desired type or convert it?

  • default discord avatar
    minsomailast year
    payload
        .find<'news'>({
          collection: 'news',
          sort: '-createdAt',
        })

    @361244678802374657
  • default discord avatar
    normanprice1last year

    Still getting same type

  • default discord avatar
    minsomailast year

    I think it's because of this



     .then((res) => res.docs.filter((doc) => doc._status === 'published'));


    do this after the query. in another variable

  • default discord avatar
    normanprice1last year

    same

  • default discord avatar
    minsomailast year

    you can put this (

    doc._status === 'published'

    ) in the where query btw. and if you want all the news without limit, you have to pass

    pagination: false

    try passing empty

    where
  • default discord avatar
    normanprice1last year

    empty?

  • default discord avatar
    minsomailast year

    where: {},

  • default discord avatar
    normanprice1last year
  • default discord avatar
    minsomailast year

    are you using latest version of payload 3?

  • default discord avatar
    normanprice1last year

    yes, beta.47

  • default discord avatar
    minsomailast year

    what is your file extension type.



    is it

    .ts

    or

    .js

    nvm

  • default discord avatar
    normanprice1last year

    .tsx

  • default discord avatar
    minsomailast year

    .47, this is working good in my case

  • default discord avatar
    normanprice1last year

    Can you share your tsconfig?



    and typescript version

  • default discord avatar
    minsomailast year
    https://github.com/payloadcms/payload-3.0-demo

    I cloned this and updated to .47

  • default discord avatar
    normanprice1last year

    weird



    Ok, it turns out that this is problem only with vscode. I opened project in webstorm and it returns correct type



    And thanks for your help

  • default discord avatar
    rubbrdcklast year

    I am experiencing the same issue on vs code and tried payload versions beta.40 and beta.53.



    No luck.


    Any ideas?

  • default discord avatar
    fturmellast year

    Got the same error this morning updating to TypeScript 5.5. The root cause seem to be a Duplicate identifier error in the Payload generated types. I've reported it in the core-dev channel. If you go back to TS 5.4 it should be OK.

  • default discord avatar
    rubbrdcklast year

    I've rollbacked to TS 5.4.5 without any luck..

  • default discord avatar
    minsomailast year
      const providerConfig = await payload.findGlobal({
        slug: "auth",
        depth: 2,
      }) as unknown as Auth;

    I'm doing this as alternative for now. LMK if you guys got better fix



    https://github.com/payloadcms/payload/pull/6385

    looks like fixed in beta.



    https://discord.com/channels/967097582721572934/1102950643259424828/1254869229312802876

    read the discussion here

  • default discord avatar
    steven093441_65645last year

    I've the same problem after updating VsCode, but with payload version 2. Anyone know how to fix this?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.