Skip to content

how generic type and type inference works changed #33278

Closed
@gogoyqj

Description

@gogoyqj

TypeScript Version: 3.6.3+

Search Terms:

Code
(https://github.com/gogoyqj/tkit/blob/master/packages/model/__tests__/createModelSamples/testPutCallInModelOK.ts)

createModel
[model example]

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
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 model = createModel({
  namespace: 'model',
  state: modelWithEffectsState,
  reducers: {
    doSomething: (state, action: { id: number }) => ({ ...state })
  },
  effects: {
  }
});

const { actions } = model;
actions.doSomething({ id: 1 });

Expected behavior:

from 3.3.+ to 3.5.,

while invoking createModel, the pass-in object's properties state's type determines reducers['doSomething'] first argument's type and its type is the type of modelWithEffectsState,

and reducers['doSomething'] second argument's type is the type of model.actions.doSomething's argument type - really wonderful features by generic and type inference

Actual behavior:

while since 3.6.3 reducers['doSomething'] first argument's type becomes any,

is there any way to recover these features, or changes i can make to make createModel works again?

help needed

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions