[英]What does type Props mean in react and typescript when used with Omit?
I am new to using typescript and react-dates package.我是使用 typescript 和 react-dates 包的新手。
I am not sure what the below type means我不确定下面的类型是什么意思
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>
);
}
What does the type Props
with Omit
do here in this case?在这种情况下,带有
Omit
的类型Props
在这里做什么?
This line function DateRangePicker(props: Props)
means that DateRangePicker
component props should be compatible with Props
type that is defined earlier.这行
function DateRangePicker(props: Props)
意味着DateRangePicker
组件 props 应该与之前定义的Props
类型兼容。
And this block而这块
type Props = Omit<
DayPickerRangeControllerShape,
| 'numberOfMonths'
| 'focusedInput'
| 'onFocusChange'
| 'hideKeyboardShortcutsPanel'
| 'noBorder'
>;
means that type Props
should be equal to DayPickerRangeControllerShape
interface, but without some of the fields, like numberOfMonths
, focusedInput
and etc.意味着类型
Props
应该等于DayPickerRangeControllerShape
接口,但没有一些字段,如numberOfMonths
, focusedInput
等。
Omit
basically strips (removes) some fields from interface or type which you pass to it. Omit
基本上从你传递给它的接口或类型中剥离(删除)一些字段。
Stating Typescript,说明打字稿,
Omit<Type, Keys>
Constructs a type by picking all properties from Type and then removing Keys.Omit<Type, Keys>
通过从 Type 中选取所有属性然后移除 Keys 来构造一个类型。
Basically, you can use Omit
to create a new type from an already existing type by removing certain properties.基本上,您可以使用
Omit
通过删除某些属性从现有类型创建新类型。
For example,例如,
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.
在 react 中,props 代表属性,它通常从一个组件传递到另一个组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.