Tslint: `no-unnecessary-callback-wrapper` false positive (mismatched args)

Created on 29 Mar 2017  ยท  7Comments  ยท  Source: palantir/tslint

Bug Report

TypeScript code being linted

function a(arg1: string, arg2?: number) {
}

function b(callback: (arg1: string, arg2?: string) => void) {
}

b((arg1) => a(arg1)); // rule warns on this...
b(a);                 // ...but this isn't valid

with tslint.json configuration:

"no-unnecessary-callback-wrapper": true

Actual behavior

If the 2nd args are optional and different types, the rule flags this, but it can't be fixed by unwrapping

Expected behavior

Shouldn't warn, but I don't think this can be fixed without type checking

P3 Requires Type Checker Bug ๐ŸŒน R.I.P. ๐ŸŒน

Most helpful comment

This rule breaks several window methods, for example:

const foo = {
    bar: (cb) => setTimeout(cb)
}

which becomes

const foo = {
    bar: setTimeout
}

And foo.bar() throws TypeError: illegal invocation.

This thing happens with all window methods like setTimeout, clearTimeout, etc.

All 7 comments

The classic example of this is map and parseInt:

> ['010', '010', '010'].map(x => parseInt(x))
[10, 10, 10]
> ['010', '010', '010'].map(parseInt)
[10, NaN, 2]

Same problem

image

image

This rule breaks several window methods, for example:

const foo = {
    bar: (cb) => setTimeout(cb)
}

which becomes

const foo = {
    bar: setTimeout
}

And foo.bar() throws TypeError: illegal invocation.

This thing happens with all window methods like setTimeout, clearTimeout, etc.

I made a small reproducement repo for a simplified case of something we ran into in our code.

await new Promise<void>(res => {
  document.addEventListener('click', () => res());
});

Replacing () => res() with res results in a Typescript error (Event and void is not compatible for the Promise).

https://github.com/csvn/tslint-issue-no-unnecessary-callback-wrapper

Is there a workaround for this issue?

๐Ÿ’€ _It's time!_ ๐Ÿ’€

TSLint is deprecated and no longer accepting pull requests other than security fixes. See #4534. โ˜ ๏ธ
We recommend you instead use typescript-eslint to lint your TypeScript code with ESLint. โœ…

๐Ÿ‘‹ It was a pleasure open sourcing with you!

๐Ÿค– Beep boop! ๐Ÿ‘‰ TSLint is deprecated ๐Ÿ‘ˆ _(#4534)_ and you should switch to typescript-eslint! ๐Ÿค–

๐Ÿ”’ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avanderhoorn picture avanderhoorn  ยท  3Comments

cateyes99 picture cateyes99  ยท  3Comments

SwintDC picture SwintDC  ยท  3Comments

allbto picture allbto  ยท  3Comments

dashmug picture dashmug  ยท  3Comments