{
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
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
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! ๐