鍍金池/ 問答/HTML/ typescript+react 如何在不使用PropTypes的情況下,可以使

typescript+react 如何在不使用PropTypes的情況下,可以使導(dǎo)出的組件智能提示Props?

我們知道在ES6 React中使用PropTypes,如果組件定義了proptypes,在組件被導(dǎo)出后IDE是能夠提示我們組件有用的props類型,這在多人協(xié)同開發(fā)的時候是非常有用的,在TypeScript中,如何實現(xiàn)該功能?是我的代碼寫的不對?還是IDE缺少插件支持? 忘了說IDE用的WebStorm,如您不吝指點,不勝感激~

代碼如下


export interface NewSwitchProps extends BaseControlProps{
    size?: 'small' | 'default';
    checked?: boolean;
    defaultChecked?: boolean;
    onChange?: (checked: boolean) => any;
    checkedChildren?: React.ReactNode;
    unCheckedChildren?: React.ReactNode;
    disabled?: boolean;

}

export class NewSwitchControl extends Component<NewSwitchProps, any> {
    static defaultProps = {
        hidden: true
    }
    render(){
        const {label, hidden, ...restProps} = this.props;
        return (
            <BaseControl label={label} hidden={hidden || true} >
                <Switch {...restProps} />
            </BaseControl>
        )
    }
}

我把TSX文件導(dǎo)入到j(luò)s文件后,并沒有出現(xiàn)props的智能提示,求指點

回答
編輯回答
葬憶

react typescript可以通過interface來約定react組件props,還有必要用react的propTypes嗎?

2018年8月23日 23:07
編輯回答
焚音

你的問題里面也描述了,你把TSX文件導(dǎo)入到j(luò)s文件后,并沒有出現(xiàn)props的智能提示,這是肯定的啊,提示是ts的語言服務(wù)帶來的功能,只能用在ts文件上,如果你是把一個tsx文件導(dǎo)入到ts文件或者tsx文件都會有語法提示的

2018年4月25日 14:44