简体   繁体   English

在 Next.js 页面中正确键入道具

[英]properly typing props in a Next.js page

I have a fairly straight forward SSR-generated Next.js page.我有一个相当直接的 SSR 生成的 Next.js 页面。 My typing is not correct somewhere along the path here, and the linter is complaining.我的打字在这里路径的某个地方不正确,并且 linter 在抱怨。

export interface ProposalTag {
  id: number;
  name: string;
  hex: string;
  color: string;
}
export async function getProposalTags(): Promise<ProposalTag[]> {
  // query for response

  const tags = response?.data?.proposal_tags.map((tag: ProposalTag) => {
    return {
      id: tag.id,
      name: tag.name,
      hex: tag.hex,
      color: tag.color,
    };
  });

  return tags;
}



export const getServerSideProps: GetServerSideProps = async context => {
  const proposalTags: ProposalTag[] = await getProposalTags();

  return {
    props: {
      proposalTags,
    },
  };
};

const Proposal: NextPage = ({ proposalTags }) => { /* code */ }

In this particular case, the linter is complaining with the following:在这种特殊情况下,linter 抱怨以下内容:

Property 'proposalTags' does not exist on type '{}'

I'm not entirely sure what I can do to please the typescript interpreter/linter here我不完全确定我能做些什么来取悦 typescript 解释器/linter

NextPage is a generic type with optional generic parameters, and the first one is the prop types for the component: NextPage是具有可选泛型参数的泛型类型,第一个是组件的 prop 类型:

const Proposal: NextPage<{
    proposalTags: ProposalTag[];
}> = ({ proposalTags }) => { /* code */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM