西安网站建设 app,深圳设计展,学ps可以从事什么工作,广州网站建设(信科分公司)使用 TS 泛型来定义通用参数
有些时候会遇到有些业务页面结构是通用的#xff0c;只是传入页面组件的参数略有不同#xff0c;这样我们可以使用 TS 的泛型来定义通用参数。具体的实例如下#xff1a;
type GenericPropsDataT {items: T[];onClick: (value: T) 只是传入页面组件的参数略有不同这样我们可以使用 TS 的泛型来定义通用参数。具体的实例如下
type GenericPropsDataT {items: T[];onClick: (value: T) void;
};const GenericProps: React.FCGenericPropsDatanumber | string ({items,onClick,
}) {return (divh2TS 泛型定义参数/h2{items.map((item) {return (div key{item} onClick{() onClick(item)}{item}/div);})}/div);
};export default GenericProps;上述例子中只是简单列举了泛型为数字或者字符串泛型还可以定位为其他对象。
使用 TS 限定传入的参数
有些业务场景要求根据在一定条件下传入对应参数组件中的其他参数为不能传递的情况或者出现类型的情况时我们可以考虑使用 TS 的never和联合类型来声明定义参数的组件。具体实例如下
type RandomNumberType {value: number;
};type PositiveNumber RandomNumberType {isPositive: boolean;isNegative?: never;isZero?: never;
};type NegativeNumber RandomNumberType {isNegative: boolean;isPositive?: never;isZero?: never;
};type Zero RandomNumberType {isZero: boolean;isPositive?: never;isNegative?: never;
};type RandomNumberProps PositiveNumber | NegativeNumber | Zero;const RestrictionProps: React.FCRandomNumberProps ({value,isPositive,isNegative,isZero,
}) {return (div{value} {isPositive 整数} {isNegative 负数} {isZero 0}/div);
};export default RestrictionProps;使用 TS 的 Exclude 去除某些联合类型
我们可以使用 TS 中的 Exclude 来去除某些联合类型的参数例如下面实例
type HorizontalPosition left | center | right;
type VerticalPosition top | center | bottom;/*** 组件传入的参数可以有如下这些* left-center | left-top | left-bottom | center | center-top |* center-bottom | right-center | right-top | right-bottom** 我们通过Exclude抛掉了center-center的值*/
type ToastProps {position:| Exclude${HorizontalPosition}-${VerticalPosition}, center-center| center;
};const ExcludeProps: React.FCToastProps ({ position }) {return divToast Notification Position - {position}/div;
};export default ExcludeProps;使用 TS 实现多态组件
type TextOwnPropsE extends React.ElementType {size?: sm | md | lg;color?: primary | secondary;children: React.ReactNode;as?: E;
};type TextPropsE extends React.ElementType TextOwnPropsE OmitReact.ComponentPropsE, keyof TextOwnPropsE;const PolymorphismProps: React.FCTextPropsReact.ElementType ({size,color,children,as,
}) {const Component as || div;return (Component className{class-with-${size}-${color}}{children}/Component);
};export default PolymorphismProps;