问题描述
我是刚开始使用Typescript和react-dates包的人。
我不确定以下类型的含义
type Props = Omit<
DayPickerRangeControllerShape,| 'numberOfMonths'
| 'focusedInput'
| 'onFocusChange'
| 'hideKeyboardShortcutsPanel'
| 'noBorder'
>;
function DaterangePicker(props: Props) {
return (
<Wrapper>
<DayPickerRangeController
{...props}
numberOfMonths={3}
onFocusChange={noop}
hideKeyboardShortcutsPanel
noBorder
horizontalMonthPadding={32}
dayPickerNavigationInlinestyles={{
display: 'flex',justifyContent: 'space-between',}}
</Wrapper>
);
}
解决方法
此行function DateRangePicker(props: Props)
意味着DateRangePicker
组件道具应该与先前定义的Props
类型兼容。
这个方块
type Props = Omit<
DayPickerRangeControllerShape,| 'numberOfMonths'
| 'focusedInput'
| 'onFocusChange'
| 'hideKeyboardShortcutsPanel'
| 'noBorder'
>;
类型为Props
的方法应等于DayPickerRangeControllerShape
接口,但没有某些字段,例如numberOfMonths
,focusedInput
等。
Omit
基本上从接口或输入类型中删除(删除)了某些字段。
统计打字稿,
Omit<Type,Keys>
通过从中选择所有属性来构造类型 键入然后删除密钥。
基本上,您可以使用Omit
通过删除某些属性从现有类型创建新类型。
例如,
interface Employee {
id: number;
name: string;
}
type EmployeeWithoutName = Omit<Employee,"name">;
const employee: Employee = {
id: 1
name: "Richard"
};
const employeeWithoutName: EmployeeWithoutName = {
id: 1
};
employeeWithoutName.name = "Jones"; // Throws an error.
,
在反应中,道具代表属性,通常将其从一个组件传递到另一组件。