React-Select在Typescript中导入失败

4

我正在尝试使用React-Select进行设置。它在标准的React项目中运行良好,但在TypeScript React项目中(均使用带有webpack的Create-React-App),却无法正常工作。

在我的TypeScript项目中,它显示“Select”未被导出。我可以选择“SelectBase”,但它不能按预期工作。

已执行的步骤: 我通过npm安装了react-select和@types/react-select。

在我的组件中,当尝试从“react-select”中导入Select时,我会在“Select”上看到TypeScript的下划线,并出现错误(请参见下文)。

使用的配置:

npm install --save react-select
npm install --save-dev @types/react-select

import Select from 'react-select';  (errors here)

错误:

Module '"../../...project.../node_modules/@types/react-select"' has no exported member 'Select'.      ts(2305)


Module '"c:/...project.../node_modules/@types/react-select/index"' has no exported member 'Select'.      ts(2305)

文档只展示了使用yarn进行安装,但我想使用npm也可以。 我需要进行不同的配置吗? 谢谢


1
根据您的TypeScript配置,您可能需要使用* as结构导入它:import * as Select from 'react-select'; - Richard
你有检查这个问题吗? https://github.com/JedWatson/react-select/issues/3201 - Laura
Richard- 我试过了。我不再遇到导入错误,但我仍然无法使用“Select”。按照我理解的方式,使用该语法,“Select”代表react-select包中的所有内容,而不是实际的“Select”模块。因此,在我的代码中,我可以引用“Select. _____”。我可以在空白行中填写特定的模块,但真正的“Select”模块仍然不是一个选项。谢谢! - Josiah Monks
Laura,谢谢。我看到了那个。答案是不仅要安装类型,还要安装包本身。我已经两者都做了。谢谢! - Josiah Monks
2个回答

1

如果在导入import { components, ControlProps, Props, Select, StylesConfig, } from "react-select";时遇到问题,那是因为Select是react-select的默认导出,可以通过以下方式解决:import Select, { components, ControlProps, Props, Select, StylesConfig, } from "react-select";


0
好的,我搞定了。 我注意到 TypeScript 在处理其他之前都没问题的导入时也出现了错误。因此问题不是 react-select,而是我的 TypeScript 配置中出现了更深层次的错误。我想可能是在解决其他问题时出了点差错。 我回到了之前的版本,安装了 react-select,然后它就可以正常工作了。对造成的麻烦很抱歉!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接