Tslint: no-inferred-empty-object-type not detecting all generics

Created on 29 Oct 2017  ยท  8Comments  ยท  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.8.0
  • __TypeScript version__: 2.5.3
  • __Running TSLint via__: (pick one) CLI

TypeScript code being linted

    {
        const wrapper = <L, A>(l: L) => ({ l });
        const x = wrapper(1); // expected error, no error
        // The second generic, A, is inferred as an empty object
    }

with tslint.json configuration:

{
    "rules": {
        "no-inferred-empty-object-type": true
    }
}

/cc @rsxdalv

Bug ๐ŸŒน R.I.P. ๐ŸŒน

All 8 comments

Is this just an overly simplified example? A is never used anywhere

@ajafff That's true, however I still want tslint to tell me about the inferred empty object, even if the generic is unused in the function definition.

The generics could be used in the return type, but tslint still does not error:

    class Wrapper<L, A> {
        _L: L;
        _A: A;
    }
    const wrapper = <L, A>(l: L): Wrapper<L, A> => new Wrapper();
    const x = wrapper(1); // expected error, no error

For context, an example of a real world function like this is the Either type https://github.com/gcanti/fp-ts/blob/c2caee7ca5da32651c0d106d2232d76f9d15a133/src/Either.ts#L301

@OliverJAsh There's definitely bugs in this one.

To me it seems that return type enables this rule to function, so this one does error (however, isn't useful):

const wrapper = <L, A>(l: A): L => ({ l });
const x = wrapper(1); // error 
// x: {}

Edit: However any deviations from exact "{}" fail. Such as:

const wrapper = <L, A>(l: L): [L, A] => ({ l } as any);
const x = wrapper(1); // expected error, no error
// x: [number, {}]

I have it working in a branch https://github.com/ajafff/tslint/commit/9635993f5a7bb0e9b2b97ec3e49c6a914de3d5de

Unfortunately this makes it a bit too strict IMO. If an optional parameter is not passed, it's TypeParameter will be inferred as empty object.
Consider the following code:

declare function fn<T>(obj?: T, cb?: (obj: T) => void): void;

fn(); // error here, because T is inferred as {}

@ajafff Personally that is desired behaviour. I want to know when empty object is inferred regardless of how.

I polished up my implementation and submitted #3445 to fix 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