-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pick<Type, GenericParameter> throws new errors for attempting to access properties #28719
Comments
This implementation seems unsound given the follow instantiation. interface NotFoo {
a: number,
b: string;
}
const notFoo: NotFoo = { a: 42, b: "not-boolean" };
SomeFunc<'a'>(notFoo); |
That's plausible. Currently, the function in question in our code base is trying to pass the parameter on to React's // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
// See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
// Also, the ` | S` allows intellisense to not be dumbisense
setState<K extends keyof S>(
state: ((prevState: Readonly<S>, props: P) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null),
callback?: () => void
): void; In our actual usage, we're seeing if a property exists on the parameter; if so, we add more properties before calling setState: private setStateBasedOnMode = <K extends keyof DateSelectorState>(
state: Pick<DateSelectorState, K>
) => {
if (state.currentSelectorMode) {
// update the shownViewStates if the currentSelectorMode is changed
let shownViewStates = this.getShownViewState(state.currentSelectorMode);
state.showEndTime = shownViewStates.showEndTime;
state.showDuration = shownViewStates.showDuration;
state.showEndDate = shownViewStates.showEndDate;
state.showTimeOrDuration = shownViewStates.showTimeOrDuration;
state.showTimeZones = shownViewStates.showTimeZones;
}
this.setState(state);
}; Is this something that would simply need to be fixed in |
@ahejlsberg we saw that strange pattern when we reviewed the rwc baselines for your change, if I recall. Did we decide what the better way to write that in a typesafe way was? Off the cuff I'd say a spread'd be better, personally. |
Agreed. A particularly troublesome aspect of the code example above is that it mutates the |
Spread where, exactly? To get it to work, I still seem to have to type-cast it: private setStateBasedOnMode = <K extends keyof DateSelectorState>(
state: Pick<DateSelectorState, K>
) => {
if ((state as DateSelectorState).currentSelectorMode) {
// update the shownViewStates if the currentSelectorMode is changed
let shownViewStates = this.getShownViewState((state as DateSelectorState).currentSelectorMode);
this.setState({
...state,
showEndTime: shownViewStates.showEndTime,
showDuration: shownViewStates.showDuration,
showEndDate: shownViewStates.showEndDate,
showTimeOrDuration: shownViewStates.showTimeOrDuration,
showTimeZones: shownViewStates.showTimeZones,
});
} else {
this.setState(state);
}
}; |
private setStateBasedOnMode = (
state: Partial<DateSelectorState>
) => {
if (state.currentSelectorMode) {
// update the shownViewStates if the currentSelectorMode is changed
let shownViewStates = this.getShownViewState(state.currentSelectorMode);
this.setState({
...state,
showEndTime: shownViewStates.showEndTime,
showDuration: shownViewStates.showDuration,
showEndDate: shownViewStates.showEndDate,
showTimeOrDuration: shownViewStates.showTimeOrDuration,
showTimeZones: shownViewStates.showTimeZones,
});
} else {
this.setState(state);
}
}; ? |
Unfortunately, no. 😔
|
Also ran into this today when upgrading typescript. Similar use-case where I'm accepting an argument that gets passed to React's setState with potentially additional data added depending on which fields are passed in. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
TypeScript Version: 3.2.0-rc, 9319ea4
Search Terms:
Pick, keyof
Code
Expected behavior:
No error (passes in 3.1.6)
Actual behavior:
Playground Link:
Related Issues:
Possibly related to #28647, but I'm unsure.
The text was updated successfully, but these errors were encountered: