Skip to content
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

inline function implicit any support #75

Closed
ron23 opened this issue Dec 15, 2020 · 2 comments
Closed

inline function implicit any support #75

ron23 opened this issue Dec 15, 2020 · 2 comments

Comments

@ron23
Copy link

ron23 commented Dec 15, 2020

I think it makes sense to not add any to inline functions and it's better to leave them for TS to manually infer. It makes migration a bit easier and safer. Example:

// utils.ts  =< TS file
function addOne(input: number): number {
  return input + 1;
}

// foo.js
function f(input) {
  input.map(x => addOne(x))
}

### after ts-migrate runs (foo.ts)

function f(input: any) {
  input.map((x: any) => addOne(x));   // <= I want to keep x implicit
}

// when I type `input` I don't get error on the inline
function g(input: string[]) {
  input.map((x: any) => addOne(x)); // no error
}

// What I want: adding types to f will infer correctly and detect an error
function f(input: string[]) {
  input.map(x => addOne(x)); // Argument of type 'string' is not assignable to parameter of type 'number'.(2345)
}
@edsrzf
Copy link
Collaborator

edsrzf commented Dec 16, 2020

The reason that ts-migrate adds the any is because TypeScript is still flagging an error with the noImplicitAny: true option. This link might make it clearer: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAQwCaoPJgKYAoZgAOIUAXImCALYBG2ATgJTmW0OIDeAUIovdlBD0kBYlEQBqRAEYA3FwC+XLqEiwEiYPiIlyyMAE9GnHolEkAdFWSFcAD0QBeAHwp0WPHcaN5CoA

You're correct that this is a little annoying when you do decide to add the : string[] annotation and the annotation for the map callback becomes unnecessary.

I'm not sure of the best way to solve this, though. It would be nice if the TypeScript compiler could report unnecessary annotations, maybe.

@ron23
Copy link
Author

ron23 commented Dec 17, 2020

I see, yea that makes sense. Thanks. I'll close.

@ron23 ron23 closed this as completed Dec 17, 2020
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

No branches or pull requests

2 participants