-
Notifications
You must be signed in to change notification settings - Fork 12.8k
how generic type and type inference works changed #33278
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
and in simpler scenario it works ok function caller2<
M,
P extends {
[doSomething: string]: <P extends any>(state: Readonly<M>, action: P) => M;
}
>(o: { state: M; reducers: P }) {
return o;
}
caller2({
state: { name: '2' },
reducers: {
'2': state => ({ ...state, name: '' })
}
}); and little complex fail function caller2<
M,
P extends {
[doSomething: string]:
| (<P extends never>(state: Readonly<M>, action: P) => M)
| (<P extends{}>(state: Readonly<M>, action: P) => M);
}
>(o: { state: M; reducers: P }) {
return o;
}
caller2({
state: { name: '2' },
reducers: {
'2': state => ({ ...state, name: '' })
}
}); |
@gogoyqj This might be fixed in 3.6.3. Can you try typescript@next? |
@sandersn,thanks for ur suggestion and i change the generic from:export interface Reducers<M> {
[doSomething: string]:
| (<P extends never>(state: Readonly<M>, action: P) => M)
| (<P extends AbstractAction>(state: Readonly<M>, action: P) => M);
} to:export interface Reducers<M> {
[doSomething: string]:
(<P extends AbstractAction>(state: Readonly<M>, action: P) => M);
} then it works - so does typescript version 3.6.2 |
Yeah, I can confirm that for type Effects = {};
type AbstractAction = {};
export interface Reducers<M> {
[doSomething: string]:
| (<P extends never>(state: Readonly<M>, action: P) => M)
| (<P extends AbstractAction>(state: Readonly<M>, action: P) => M);
}
export default function createModel<M, R extends Reducers<M>, E extends Effects>(model: {
namespace: string;
state: M;
reducers: R;
effects?: E;
}) {
}
const modelWithEffectsState = {};
const model = createModel({
namespace: 'model',
state: modelWithEffectsState,
reducers: {
doSomething: (state, action: { id: number }) => ({ ...state })
},
effects: {
}
}); in 3.5, |
Our changes to |
Hmmm, so sadly I think I can only say that this really only worked essentially because of the bug fixed in #32049 - the two signatures really aren't identical - even if we try to do better and produce inferences for |
seems in early version[ 3.3?] doSomething: state => ({ ...state }) is not compatible with: doSomething: (<P extends AbstractAction>(state: Readonly<M>, action: P) => M) but compatible with: doSomething: (<P extends never>(state: Readonly<M>, action: P) => M) that's why i using union | (<P extends never>(state: Readonly<M>, action: P) => M)
| (<P extends AbstractAction>(state: Readonly<M>, action: P) => M) and since version 3.4, 3.5, 3.6 are both compatible - but 3.6 works little differently honestly maybe it's a bravo improvement not a bug since version 3.4 and i can simplify my code |
TypeScript Version: 3.6.3+
Search Terms:
Code
(https://github.com/gogoyqj/tkit/blob/master/packages/model/__tests__/createModelSamples/testPutCallInModelOK.ts)
createModel
[model example]
Expected behavior:
from 3.3.+ to 3.5.,
while invoking
createModel
, the pass-in object's propertiesstate
's type determinesreducers['doSomething']
first argument's type and its type is the type ofmodelWithEffectsState
,and
reducers['doSomething']
second argument's type is the type ofmodel.actions.doSomething
's argument type - really wonderful features by generic and type inferenceActual behavior:
while since 3.6.3
reducers['doSomething']
first argument's type becomesany
,is there any way to recover these features, or changes i can make to make
createModel
works again?help needed
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: