Skip to content

Typings for attach fails if specify effect argument type #439

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

Closed
PFight opened this issue Dec 30, 2020 · 6 comments
Closed

Typings for attach fails if specify effect argument type #439

PFight opened this issue Dec 30, 2020 · 6 comments
Labels
core effector package typings Typescript public type definitions issues

Comments

@PFight
Copy link

PFight commented Dec 30, 2020

What is the current behavior:

There exact sample from https://effector.dev/docs/api/effector/attach, but with one change - when calling createEffect I added type for effect argument:

         const original = createEffect<string, void>(params => {
            console.log('Original effect called with', params);
        });
        
        const data =createStore(8900);
        
        const created = attach({
            effect: original,
            source: data,
            mapParams: (params, data) => {
                console.log('Created effect called with', params, 'and data', data);
                return {wrapped: params, data};
            },
        });

TypeScript 3.8.0 compailns with error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ effect: Effect<string, void, Error>; source: Store<number>; mapParams: (params: unknown, data: number) => { wrapped: unknown; data: number; }; }' is not assignable to parameter of type '{ effect: Effect<string, void, Error>; }'.
      Object literal may only specify known properties, and 'source' does not exist in type '{ effect: Effect<string, void, Error>; }'.

Which versions of effector packages, and which browser and OS are affected by this issue? Did this work in previous versions of effector?:

"effector": "^21.7.5",
TypeScript 3.8.0

Maybe the last TypeScript will be ok, can't check right now.

@PFight PFight added bug Something isn't working needs triage labels Dec 30, 2020
@zerobias
Copy link
Member

Looks like you have type error: mapParams returns {wrapped: any; data: number} but original effect accepts only string. Typescript prefer to look at mapParams at first, so its message rather out of place

However, explicit generics usually reveal actual messages:

const original = createEffect((params: string) => {
    console.log('Original effect called with', params)
  })

const data = createStore(8900)

const created = attach<number, typeof data, typeof original>({
  effect: original,
  source: data,
  mapParams: (params, data) => {
    console.log('Created effect called with', params, 'and data', data)
    return {wrapped: params, data}
  },
})

@PFight
Copy link
Author

PFight commented Jan 11, 2021

Hm, really it was error in typings. Quite dificult to determine error source until write explicit typings... Strange typescript behaviour...

Thanks.

@PFight PFight closed this as completed Jan 11, 2021
@PFight
Copy link
Author

PFight commented Jan 11, 2021

I found, that problem with typing solves if rename overloads of attach with { source, effect, mapParams } arguments to another function name. For example, call it attachToSource. Then typescript error messages become clear and usefull.

Ho about to extract such attachToSource function?

@PFight PFight reopened this Jan 11, 2021
@zerobias
Copy link
Member

Separate methods overwhelm users, there is a direct correlation between amount of methods in api and „this is a large api“ feeling, so I'd rather try to improve inference for attach itself

@zerobias zerobias added core effector package typings Typescript public type definitions issues and removed bug Something isn't working needs triage labels Jan 12, 2021
@zerobias
Copy link
Member

Good news! I found a way to show useful error message in your case

Will be released in effector 21.8.0

@PFight
Copy link
Author

PFight commented Jan 19, 2021

Interesting solution, saved for myself. Thanks!

@PFight PFight closed this as completed Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core effector package typings Typescript public type definitions issues
Projects
None yet
Development

No branches or pull requests

2 participants