将 ReactJS 组件导入到 typescript 组件

Import ReactJS component to typescript component

提问人:samiaj 提问时间:11/10/2023 更新时间:11/14/2023 访问量:55

问:

背景:我正在处理一个现有项目,该项目已经有一些可用或在 ReactJS 中创建的组件。最近,我们决定使用 React 迁移到 TypeScript。现在,我正在尝试弄清楚如何将现有组件导入到新的打字稿组件中。这个博客帮助我弄清楚了如何做到这一点。但是我无法让它适用于我的项目中可用的某些组件。

注意:下面给出的代码块是为了解释上下文,现在是我们如何使用它。

下面的 ReactJS 组件已经存在于项目中,文件名:SelectOption.js

function SelectOption({
  list,
  defaultValue,
  placeholder,
  inputName,
  testid,
  isRequired,
  propertyID,
  propertyName,
}) {
  return (
    <div>
      <label
        htmlFor={`${testid}.id`}
        slot='label'
        className={Styles.label_padding}
      >
        {Resources.TIME_OFF.BATCH_ACTIONS_WORLKFLOW_APP.TIME_OFF_TYPE}
      </label>
      <select
        id={`${testid}.id`}
        name={inputName}
      >
        <option  value=''>
          {placeholder}
        </option>
        {list.map((item) => (
          <option
            value={item[propertyID]}
            key={item[propertyID]}
          >
            {item[propertyName]}
          </option>
        ))}
      </select>
    </div>
  );
}

export { SelectOption };

我创建了下面的声明文件,使其类型兼容并将其导入到新的打字稿组件中,文件名:SelectOption.d.ts

type listItem = { ID: number; Name: string };

type listItems = listItem[];
function SelectOption({
  list: listItems,
  defaultValue: number,
  placeholder: string,
  inputName: string,
  testid: string,
  isRequired: boolean,
  propertyID: string,
  propertyName: string
}): JSX.Element;

export {SelectOption};

下面是新的 Typescript 组件

import { SelectOption } from '../../shared/components/SelectOption/SelectOption'

export function NewTypeScriptComponent() {
    const listItems: Array<{ ID: number; Name: string }> = [
        { ID: 0, Name: 'Available' },
        { ID: 1, Name: 'Ready' },
        { ID: 2, Name: 'Started' },
    ];
    return (
        <section>
            <div>
                <SelectOption
                    list={listItems}
                    defaultValue={0}
                    placeholder={'type'}
                    inputName={''}
                    testid={true}
                    isRequired={true}
                    propertyID='ID'
                    propertyName='Name'
                />
            </div>
        </section>
    );
}

功能工作正常。我可以看到下拉列表正确地列出了新组件中的给定值,但它给出了一个错误,如下图所示

enter image description here

如果我评论/删除第一个参数(列表),那么第二个参数(defaultValue)将显示相同的错误

任何帮助将不胜感激,以了解我在这里做错了什么。 还有一个问题:这是将 ReactJS 组件导入打字稿的好方法吗? 提前致谢。

javascript reactjs 打字稿 react-typescript

评论

0赞 Dean Van Greunen 11/10/2023
更改为list={listItems}list={listItems as any}
1赞 Hiba Youssef 11/10/2023
也许您必须将 SelectOption 中的列表更改为列表:listItems |任何;
2赞 samiaj 11/10/2023
@DeanVanGreunen,HibaYoussef 正如我在问题中提到的,“如果我注释或删除第一个参数(列表),那么第二个参数(默认值)将显示相同的错误。它只是没有列表参数;如果我删除或注释列表参数,即使是第二个参数也会显示消息。我担心在所有参数中添加“any”是它会剥夺打字稿的好处。

答:

0赞 Lukas Verstraete 11/14/2023 #1

我不是这方面的专家,但我在本地尝试过,看起来您定义函数类型不正确。改变:

function SelectOption({
  list: listItems,
  defaultValue: number,
  placeholder: string,
  inputName: string,
  testid: string,
  isRequired: boolean,
  propertyID: string,
  propertyName: string
}): JSX.Element;

自:

function SelectOption(props: {
  list: listItems;
  defaultValue: number;
  placeholder: string;
  inputName: string;
  testid: string;
  isRequired: boolean;
  propertyID: string;
  propertyName: string;
}): JSX.Element;

这对我有用。在您的版本中,您将类型设置为变量名称。添加道具:{...在前面为组件的参数命名。