使用第三方React组件而不使用.d.ts文件

时间:2017-04-03 13:51:40

标签: reactjs typescript

我正在使用react-router v.4和typescript v.2.2。

我正在尝试使用简单的第三方组件(react-loading)作为react-router的Route中的“组件属性”。第三方组件没有@types .d.ts文件。反应路由器的类型已经到位。

我收到:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

当然,在渲染函数中放置一个断点,显示加载组件不是未定义的,而是需要有效的函数。 如果不为库编写.d.ts文件,我可以快速解决这个问题吗?

LATER EDIT :( v.2)

我已经尝试过这个fine tutorial并且在最大限度地简化我的代码之后(实际上省略了react-router ),我提出了:

index.tsx:

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import Loading from 'react-loading';

ReactDOM.render(
  <Loading type="balls" color="red"/>,
  document.getElementById('app')
);

typings / react-loading.d.ts

declare module 'react-loading' {

  import * as React from 'react';

  export interface LoadingProps {
    color?: string;
    delay?: number;
    height?: number;
    type?: string;
    width?: number;
  }

  export default class Loading extends React.Component<LoadingProps, any> {}
}

但我收到了:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

请注意,打字文件有效,因为从高度属性中删除(例如)...会提供额外的 new < / strong>错误:

[at-loader] src\index.tsx:7:3 
    TS2324: Property 'height' is missing in type 'IntrinsicAttributes & IntrinsicClassAttributes<Loading> & Readonly<{ children?: ReactNode; }> & R...'.

0 个答案:

没有答案