都2021年了还不会用typescript那就很尴尬,但不是就把文件后缀改成tsx、ts就算typescript了,得有友好的类型提示才算,不然每次调用方法还要看源文件需要哪些参数就很不方便。
在定义Dialog类时就遇到了很典型的问题,因为Dialog有不同种类的通知,然后不同通知类型的参数要求可能也各不相同。
调用接口时是这样的 Dialog.show(type,props),type参数要把通知类型列好,props要把通知所要的参数/数据列好。
很快就能根据需要定义出通知type和相关interface。
type DialogType = ‘toast’ | ‘notice’;
interface ToastProps {
content: ReactChild | String;
duration?: ToastDurationType;
position?: ToastPositionType;
style?: ViewStyle;
textStyle?: TextStyle;
onShow?: () => void;
onHide?: () => void;
}
interface NoticeProps {
key: number;
title: string;
content?: string;
url?: string;
onShow?: () => void;
onHide?: () => void;
handler?: (p?: any) => void;
}
给show方法加上参数类型后,但使用时却不是想的那样如期工作,参数提示并不全。