typescript - 扩展 TypeScript 接口后将可选属性转换为必需属性
问题描述
我有以下内置接口,其中属性value
标记为可选:
interface LiHTMLAttributes<T> extends HTMLAttributes<T> {
value?: string | ReadonlyArray<string> | number | undefined;
}
我想扩展上述接口以转换value
为必需的(强制)属性。以下是我这样做的方式:
export interface UiSidebarListItem extends React.LiHTMLAttributes<HTMLLIElement> {
id: string;
value: string | ReadonlyArray<string> | number | undefined;
active?: boolean;
className?: string;
onClick?: () => void;
}
但是,我需要value
使用 union as 再次定义属性的数据类型string | ReadonlyArray<string> | number | undefined
。
有没有更好的方法来做到这一点,而无需value
再次指定属性的类型并从扩展接口派生其类型LiHTMLAttributes
?
解决方案
推荐阅读
- python - 如何在 matplot/pyplot Python 3.7 中绘制 xyz 点
- html - 在 Angular 中有条件地设置属性绑定
- python - Discord.py 重写机器人在 heroku 中不起作用,但在本地工作
- node.js - 如何在 NodeJs 中无请求地访问会话数据?
- php - cURL POST - 您如何使用 POSTFIELDS 进行此特定查询
- javascript - TypeORM:如何创建双向的一对多关系?
- jquery - 在 bootstrap4 轮播中,如何使特定幻灯片停留更长时间?
- r - 随机森林预测模型
- highcharts - 区域图表多线渐变颜色
- pytest - 仅针对其他人的代码禁止 pytest 警告