与Omit一起使用时,Props类型在React和Typescript中意味着什么?

问题描述

我是刚开始使用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>
        );
    }

在这种情况下,Props类型的Omit在这里做什么?

解决方法

此行function DateRangePicker(props: Props)意味着DateRangePicker组件道具应该与先前定义的Props类型兼容。

这个方块

type Props = Omit<
    DayPickerRangeControllerShape,| 'numberOfMonths'
    | 'focusedInput'
    | 'onFocusChange'
    | 'hideKeyboardShortcutsPanel'
    | 'noBorder'
>;

类型为Props的方法应等于DayPickerRangeControllerShape接口,但没有某些字段,例如numberOfMonthsfocusedInput等。

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.
,

在反应中,道具代表属性,通常将其从一个组件传递到另一组件。