-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Contextual typing not working on optional members #16817
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
Comments
This isn't interface ActionsObject<State> {
[prop: string]: (state: State) => State;
}
interface Options<State, Actions> {
state?: State;
view?: (state: State, actions: Actions) => any;
actions: string | Actions;
}
declare function app<State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>): void;
app({
state: 100,
actions: {
foo: s => s
~
!!! error TS7006: Parameter 's' implicitly has an 'any' type.
},
view: (s, a) => undefined as any,
}); |
This is working as intended. The interface ActionsObject<State> {
[prop: string]: (state: State) => State
}
interface Options<State> {
state?: State;
actions?: ActionsObject<State>;
}
declare function app<State>(obj: Options<State>): void;
app({
state: 100,
actions: {
foo: s => s
},
}) If it is really important to capture the actual type of the object literal, it can be done with an intersection type: interface ActionsObject<State> {
[prop: string]: (state: State) => State
}
interface Options<State> {
state?: State;
actions?: ActionsObject<State>;
}
declare function app<State, Actions>(obj: Options<State> & Actions): void;
app({
state: 100,
actions: {
foo: s => s
},
}) (I omitted the |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
This issue is tagged incorrectly and we have a fix up, it's pretty simple. We actually do contextually type the nonunion case right (the generic's constraint is the contextual type), so it is, in fact, a bug that a union is not appropriately contextually typed. The issue is that when we get the apparent type of a contextual type, we didn't get the apparent type of all union members - we just returned the union. |
The following sample works perfectly in
noImplicitAny
unless I turn onstrictNullChecks
. In that event, thes
parameter onfoo
seems to get an implicitany
.If I make
actions
non-optional, then things work perfectly again.Context: jorgebucaran/hyperapp#87
The text was updated successfully, but these errors were encountered: