Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"Parameter implicitly has an 'any' type." error when passing action-wrapped function with implicit argument types to optional property #2670

Closed
seanchas116 opened this issue Dec 7, 2020 · 5 comments
Labels

Comments

@seanchas116
Copy link

seanchas116 commented Dec 7, 2020

Intended outcome:

action -wrapped function with implicit argument types can be assigned as an optional property of a object, and the types are correctly inferred.

import { action } from "mobx";

// Both should be OK

const tes1: {
  a: (x: number) => void;
} = {
  a: action((x) => console.log(x.toFixed()))
};

const test2: {
  a?: (x: number) => void;
} = {
  a: action((x) => console.log(x.toFixed()))
};

Actual outcome:

Implicit argument types in action -wrapped function assigned as an optional property of a object becomes any and causes an error in struct environments.

import { action } from "mobx";

// OK
const tes1: {
  a: (x: number) => void;
} = {
  a: action((x) => console.log(x.toFixed()))
};

// OK in MobX 5, Error in MobX 6 (x becomes any)
const test2: {
  a?: (x: number) => void;
} = {
  a: action((x) => console.log(x.toFixed()))
  // ↑ "Parameter 'x' implicitly has an 'any' type. ts(7006)" error in this line
};

Looks like MobX 5 works fine because undefined is added to T in this line. Adding undefined to T in Mobx6 action.d.ts fixed the problem in my environment.

How to reproduce the issue:

https://codesandbox.io/s/mobx-action-error-gnd8v

Versions

Occurs in MobX 6. MobX 5 works fine.

@kubk
Copy link
Collaborator

kubk commented Dec 7, 2020

It's better to add both undefined and null to support promises: #2213
Mobx 5 also had unit tests for such cases: #2217
But they were removed in Mobx 6

@stale
Copy link

stale bot commented Dec 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the 🚶 stale label Dec 24, 2020
@kubk
Copy link
Collaborator

kubk commented Dec 24, 2020

Can make a PR to fix it if anyone interested

@stale
Copy link

stale bot commented Dec 24, 2020

This issue has been automatically unmarked as stale. Please disregard previous warnings.

@mweststrate
Copy link
Member

Should be fixed in 6.0.5

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

No branches or pull requests

3 participants