TypeScript React Function组件名称作为类型

时间:2019-06-27 11:05:49

标签: reactjs typescript

React.js中,我有一个Function Component,当我将函数名称用作type时,会出错。

代码示例:

import * as React from "react";
import { render } from "react-dom";

interface NameViewProps {
  name: string;
}
function NameView({ name }: NameViewProps) {
  return <div>{name}</div>;
}

interface Props {
  // ts error: Cannot find name 'NameView'.ts(2304)
  children: NameView[];
}

class Demo extends React.Component<Props> {
  render() {
    return (
      <div>
        <h2>in demo component with children div array</h2>
        {this.props.children}
      </div>
    );
  }
}

如何将功能组件的名称用作type

您可以尝试codesandbox here

2 个答案:

答案 0 :(得分:0)

使用typeof(函数名称),即typeof NameView

答案 1 :(得分:0)

tt_time     hours_add
12:00:00    1
15:30:00    3
16:00:00    6

Expected Output
13:00:00
18:00:00
22:00:00