为react组件库添加typescript类型提示的方法
作者:all2005
这篇文章主要介绍了为react组件库添加typescript类型提示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
以我自己的组件react-better-countdown为例,
首先在package.json里面添加types: types/index.d.ts,
,
然后文件目录上添加对应文件夹和文件,

最后是index.d.ts文件的编写,具体看代码:
import * as React from 'react';
interface CountdownProps {
count?: number;
dayText?: string | React.ReactElement;
hourText?: string | React.ReactElement;
minuteText?: string | React.ReactElement;
secondText?: string | React.ReactElement;
callback?: Function;
className?: string;
style?: React.CSSProperties;
rest?: any
}
interface CountdownState {
count?: number;
}
declare module 'react-better-countdown' {
export default class Countdown extends React.Component<CountdownProps, CountdownState> {
state: CountdownState;
timer: null | number;
tick: () => void;
componentDidMount(): void;
componentDidUpdate(prevProps: CountdownProps): void;
componentWillUnmount(): void;
render: () => React.ReactElement;
}
}
更多详细代码看仓库:https://github.com/leeseean/react-better-countdown顺便求个Star!
到此这篇关于为react组件库添加typescript类型提示的方法的文章就介绍到这了,更多相关react组件库添加typescript类型提示内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
