Skip to content

An argument for 'reviver' was not provided. #1935

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
ntucker opened this issue Feb 9, 2023 · 4 comments · Fixed by #1936
Closed

An argument for 'reviver' was not provided. #1935

ntucker opened this issue Feb 9, 2023 · 4 comments · Fixed by #1936

Comments

@ntucker
Copy link

ntucker commented Feb 9, 2023

What happened

Upgraded from 4.2.2 to 4.2.4 to 4.3.4

TS2554: Expected 2 arguments, but got 1.

    124           fromJS([{ data: '1' }]),
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      node_modules/immutable/dist/immutable.d.ts:5163:5
        5163     reviver: (
                 ~~~~~~~~~~
        5164       key: string | number,
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
         ... 
        5166       path?: Array<string | number>
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        5167     ) => unknown
             ~~~~~~~~~~~~~~~~
        An argument for 'reviver' was not provided.

#1927 is the culprit

How to reproduce

typescript 4.9.5
immutable 4.2.4

const create = [(data: any) => data, fromJS][1];
// @ts-expect-error
create([{ data: '1' }])

https://stackblitz.com/edit/typescript-sk2pyy?file=index.ts

@ntucker
Copy link
Author

ntucker commented Feb 9, 2023

@jdeniau
Copy link
Member

jdeniau commented Feb 10, 2023

I tried some things, and it's kind of funny (or weird)

import { fromJS } from 'immutable';

const create = [(data: any) => data, fromJS][1];
create({}); // do error

const create2 = [fromJS][0];
create2({}); // works fine

const create3 =([(data: any) => data, fromJS] as const)[1];
create3({}); // works fine

const create4 = [(data: any, useless = undefined) => data, fromJS][1];
create4({}); // works fine

tsplay example

In the first example, the created array has a type like (function1|function2)[], while the second has a function1[] type and the last one is the tuple [function1, function2].

I see in your test stack that you already use a as const, but it seems that by passing the createInput function as a second parameter, it seems to force fromJS to be a precise type and loose the two possible overload here.

@jdeniau
Copy link
Member

jdeniau commented Feb 10, 2023

After some tests, I think #1936 does fix this and is a better declaration file for me :

  • it keeps the FromJS best possible type with object,
  • it fallbacks to Collection<unknown, unknown>,
  • if type is hard (like you case), it fallback to any

@ntucker
Copy link
Author

ntucker commented Feb 10, 2023

Yeah that looks like a reasonable solution.

Just to provide some clarity about the tests - while the 'as const' does wrap the whole tuple of tuples; since test.each is iterating through the first array; the function will end up acting on either - which is why it ends up in a union type. Though really I was just trying to create another function with the same usage as fromJS. (I never call it with a second arg).

I think the generation of a full union type is correct; but the fact that this breaks polymorphic functions seems to be the bug in TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants