我有一个create-react-app,它在types
中有一个src
目录,内容如下:
testapp-components.d.ts
import { ReactNode } from "react";
interface Option {
label: ReactNode;
value: string;
[key: string]: ReactNode;
}
declare module "@testApp/components";
但是,当我在组件中这样使用它时...
// @testApp/components is an internal library I've installed. OptionSelector is a custom react component.
import { OptionSelector } from @testApp/components;
... other React code ...
const [options, setOptions] = useState<Option[]>([]);
它给我以下错误:
TS7016: Could not find a declaration file for module '@testApp/components
下的 import { OptionSelector } ...
TS2304: Cannot find name Option
在useState下
如果我删除了import { ReactNode } ...
并将ReactNode
引用更改为any
,那么两个错误都消失了……
将文件名更改为.d.tsx
不能解决问题。
我也尝试过:
declare module "@testApp/components" {
export interface Option {
label: any;
value: string;
[key: string]: any;
}
};
哪个会为useState产生相同的错误,但是给我一个import { OptionSelector } ...
的新错误:
TS2305: Module @testApp/components has no exported member OptionSelector