简体   繁体   English

通过 React 组件向下传递道具时如何使用 Typescript 和接口?

[英]How to use Typescript and Interfaces when passing props down through React components?

I'm passing down a pretty large array of objects through several React components.我通过几个 React 组件传递了大量的对象。 I was wondering, what is the syntax for writing the types of all the properties in each object (the objects are nested several times)?我想知道,在每个object(对象嵌套多次)中写所有属性类型的语法是什么?

I currently have interfaces like below.我目前有如下界面。 These are two components, MainContent, which passes props down into Chart:这是两个组件,MainContent,它将 props 向下传递到 Chart 中:

MainContent component:主要内容组件:

interface ComparatorTypes {
  id: string;
  name: string;
}

interface DataTypes {
  jobId: string;
  jobTitle: string;
  descriptionUrl: string;
  totalCompensation: number;
  baseSalary: number;
  longevityPay: number;
  specialPay: number;
  allowances: number;
  paidTimeOff: number;
  holidays: number;
  retirementBenefit: Array<{
    formula: string;
    details: any;
  }>;
  healthBenefit: Array<{
    premium: number;
    details: any;
  }>;
  remoteWork: {
    isAllowed: string;
    details: any;
  };
}

interface QueryTypes {
  agencyName: string;
  id: string;
  data: DataTypes[];
}

interface params {
  comparatorData: ComparatorTypes[];
  queryData: QueryTypes[];
}

export default function MainContent({ comparatorData, queryData }: params) {
  return (
    <S.MainContentComponent>
      <Header />
      <Summary comparatorData={comparatorData} />
      <Chart queryData={queryData} />
    </S.MainContentComponent>
  );
}

and Chart component:和图表组件:


interface ComparatorTypes {
  id: string;
  name: string;
}

interface DataTypes {
  jobId: string;
  jobTitle: string;
  descriptionUrl: string;
  totalCompensation: number;
  baseSalary: number;
  longevityPay: number;
  specialPay: number;
  allowances: number;
  paidTimeOff: number;
  holidays: number;
  retirementBenefit: Array<{
    formula: string;
    details: any;
  }>;
  healthBenefit: Array<{
    premium: number;
    details: any;
  }>;
  remoteWork: {
    isAllowed: string;
    details: any;
  };
}

interface QueryTypes {
  agencyName: string;
  id: string;
  data: DataTypes[];
}

interface params {
  // comparatorData: ComparatorTypes[];
  queryData: QueryTypes[];
}
export default function Chart({ queryData }: params): JSX.Element {
...

You can see how redundant it is to be naming these giant, several-times-nested interfaces before every component that uses this array of objects.您可以看到在使用此对象数组的每个组件之前命名这些巨大的、多次嵌套的接口是多么多余。 Is this normal for Typescript? Typescript 这正常吗? Is there a better way to do something like this?有没有更好的方法来做这样的事情? Or does all this data need to be typed upon being passed down through every component?还是所有这些数据都需要在通过每个组件向下传递时进行键入?

What forces you to define these identical interfaces explictly for each component?是什么迫使您为每个组件明确定义这些相同的接口?

On the contrary, factorizing them would be the normal choice: that way, they are defined in a single place (single source of truth), and by importing them, you explictly say that you re-use the exact same types.相反,分解它们是正常的选择:这样,它们被定义在一个地方(单一的事实来源),并且通过导入它们,你明确地说你重复使用了完全相同的类型。

// Chart.tsx
export interface QueryTypes {
  agencyName: string;
  id: string;
  data: DataTypes[];
}

export interface DataTypes {
  jobId: string;
  jobTitle: string;
  // etc.
}

export default function Chart({
  queryData
}: {
  queryData: QueryTypes[];
}) {}

// Main.tsx
import Chart, { QueryTypes } from ".Chart";
import Summary, { ComparatorTypes } from "./Summary"; // Same for ComparatorTypes

export default function MainContent({
  comparatorData,
  queryData
}: {
  comparatorData: ComparatorTypes[];
  queryData: QueryTypes[];
}) {
  return (
    <S.MainContentComponent>
      <Header />
      <Summary comparatorData={comparatorData} />
      <Chart queryData={queryData} />
    </S.MainContentComponent>
  );
}

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

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