TS新手,我正在尝试创建一个类型,然后可以作为道具从另一个组件传递到这个组件,如:
<Error type={ErrorPageType.GENERIC}/>
有条件地呈现正确的视图。这是我的代码:
export enum ErrorPageType {
ACCESS = "access",
GENERIC = "generic",
}
interface ErrorProps {
type?: ErrorPageType;
}
const getErrorType = (type?: ErrorPageType) {
switch (type) {
case ErrorPageType.ACCESS:
return <AccessErrorPage />;
case ErrorPageType.GENERIC:
return <GenericErrorPage />;
default:
return <GenericErrorPage />;
}
}
const ErrorPage = (type?: ErrorProps) => {
return getErrorType(type)
};
export default ErrorPage;
我得到了一个错误:
error TS2322: Type '{ type: ErrorPageType; }' is not assignable to type '(IntrinsicAttributes & ErrorPageType.ACCESS) | (IntrinsicAttributes & ErrorPageType.FSSO) | (IntrinsicAttributes & ErrorPageType.GENERIC) | (IntrinsicAttributes & ErrorPageType.SIGNUP)'. Type '{ type: ErrorPageType; }' is not assignable to type 'ErrorPageType.SIGNUP'.
我真的不知道为什么。非常感谢您的帮助!