在Typescript模块中导入ReactNode失败

时间:2019-11-02 17:49:33

标签: reactjs typescript user-interface types typescript-typings

我有一个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

    下的
  1. 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

0 个答案:

没有答案