'React' 被声明,但它的值永远不会被读取

dag*_*da1 7 typescript

我有以下代码:

// tslint:disable
import * as React from 'react';
import { Input, InputProps } from '../atoms/Input/Input';
import { FormControl } from '../hoc/FormControl/FormControl';

export const FormInput = FormControl<InputProps>(Input);
Run Code Online (Sandbox Code Playgroud)

Typescript 不会编译它,但会抱怨:

'React' 被声明,但它的值永远不会被读取。

如果我删除该行,我会得到:

导出的变量“FormInput”具有或正在使用来自外部模块“/node_modules/@types/react/index”的名称“React.ComponentClass”,但无法命名。[ts] 导出的变量“FormInput”具有或正在使用来自外部模块“/node_modules/@types/react/index”的名称“React.StatelessComponent”,但无法命名。

declaration在 tsconfig 中设置为 true,这是正确的,因为这是一个包。

小智 6

更新 tsconfig.json

{
  "compilerOptions": {
     "jsx": "react"
  }
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*hen 0

尝试升级到 TypeScript 2.9 或更高版本。TypeScript 2.9 添加了对导入类型的支持,这应该可以删除导入React并仍然生成声明文件。