Reputation: 12197
I get confused about how to do this kind of type casting in TS. I am defining a class method that needs to be this type (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-big-calendar/index.d.ts#L229)
and I am trying to do something like this...
public onSelectSlot: Pick<BigCalendarProps<InterviewEvent>, 'onSelectSlot'> = (slot) => {
this.setCreateDialog(true, slot.slots.slice(0, -1));
}
but that is not working and I cannot figure out how to pull the function arg type out of that interface in the link. How can I go about doing this?
Upvotes: 1
Views: 1917
Reputation: 36
Pick returns a set of properties
type Pick<T, K extends keyof T> = { [P in K]: T[P]; }
So if you want to get a propertie,you need to provide the key of propertie like this
public onSelectSlot: Pick<
BigCalendarProps<InterviewEvent>,
'onSelectSlot'
>['onSelectSlot'] = slot => {};
Or you can define a type alias
type PickPropertie<T, K extends keyof T> = T[K];
and use it.
public onSelectSlot: PickPropertie<
BigCalendarProps<InterviewEvent>,
'onSelectSlot'
> = slot => {};
Upvotes: 0
Reputation: 250366
You are using Pick
, that will not return the type of the proeprty but rather an object type with the picked properties.
You can use a type query to get the type of onSelectSlot
.
public onSelectSlot: BigCalendarProps<InterviewEvent>['onSelectSlot'] = (slot) => {
this.setCreateDialog(true, slot.slots.slice(0, -1));
}
Upvotes: 2