首页 > 解决方案 > 在打字稿模块中导入 ReactNode 失败

问题描述

我有一个 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[]>([]);

它给了我以下错误:

  1. TS7016: Could not find a declaration file for module '@testApp/components在下面import { OptionSelector } ...

  2. 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 } ...

  1. TS2305: Module @testApp/components has no exported member OptionSelector

标签: reactjstypescriptuser-interfacetypestypescript-typings

解决方案


推荐阅读