Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从使用“prop类型”的JS React组件生成类型定义`_Javascript_Reactjs_Typescript_React Proptypes - Fatal编程技术网

Javascript 从使用“prop类型”的JS React组件生成类型定义`

Javascript 从使用“prop类型”的JS React组件生成类型定义`,javascript,reactjs,typescript,react-proptypes,Javascript,Reactjs,Typescript,React Proptypes,我们有一个组件,如: import PropTypes from 'prop-types'; import React from 'react'; export default class Tooltip extends React.Component { static propTypes = { /** * Some children components */ children: PropTypes.node.isRequired, /**

我们有一个组件,如:

import PropTypes from 'prop-types';
import React from 'react';

export default class Tooltip extends React.Component {
  static propTypes = {
    /**
     * Some children components
     */
    children: PropTypes.node.isRequired,
    /**
     * Some property
     */
    someProp: PropTypes.string,
   }

   .....

}
是否有一些工具支持对typescript声明文件进行类型生成,以生成如下内容:

interface ITooltip {
    children: React.Node,
    someProp: string
}
一些参考:


如果安装
@types/prop types
则可以使用
InferProps
泛型类型生成如下类型:

import { InferProps } from "prop-types"

type ITooltip = InferProps<typeof Tooltip.propTypes>

// or interfaces if that's what you prefer:
// interface ITooltip extends InferProps<typeof Tooltip.propTypes> {}
从“prop类型”导入{InferProps}
类型ITooltip=InferProps
//或接口(如果您喜欢):
//接口ITOLTIP扩展了InferrProps{}

由于键入了
prop类型,该类型更为详细,但它在使用中会给出正确的结果。

我希望有一种方法可以在生成过程中生成ts声明文件。这是一个用
js
编写的包,它被导入到
ts
项目中。TypeScript编译器正在为您执行此操作。使用
-d
--declaration
可以生成一个包含所有导出成员的
d.ts
文件。但是
tsc
将为仅通过
js
中的
道具类型定义的道具生成
d.ts
文件,这就是我正在寻找的工作流。