Skip to content

[no-unnecessary-type-assertion] false positive when asserting the result of generic function invocation #528

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

Closed
vkrol opened this issue May 14, 2019 · 2 comments
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@vkrol
Copy link
Contributor

vkrol commented May 14, 2019

Repro
https://github.com/vkrol/typescript-eslint-no-unnecessary-type-assertion-generic-function

module.exports = {
	parser: "@typescript-eslint/parser",
	parserOptions: {
		project: "tsconfig.json",
	},
	plugins: ["@typescript-eslint"],
	rules: {
		"@typescript-eslint/no-unnecessary-type-assertion": "error",
	}
};
declare function foo<T extends Element = Element>(): T;
const a = (foo() as HTMLInputElement).value;

Expected Result
No errors.
Actual Result
Error:

D:\Projects\typescript-eslint-no-unnecessary-type-assertion-generic-function\index.ts
  2:12  error  This assertion is unnecessary since it does not change the type of the expression  @typescript-eslint/no-unnecessary-type-assertion

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

Additional Info
N/A.

Versions

package version
@typescript-eslint/eslint-plugin 1.9.0
@typescript-eslint/parser 1.9.0
TypeScript 3.4.5
ESLint 5.16.0
node 10.15.3
npm 6.4.1
@vkrol vkrol added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 14, 2019
@bradzacher bradzacher added bug Something isn't working external This issue is with another package, not typescript-eslint itself wontfix This will not be worked on and removed triage Waiting for team members to take a look labels May 14, 2019
@bradzacher
Copy link
Member

bradzacher commented May 16, 2019

Looking into this, it looks like a bug (or a feature?) in typescript.

It is inferring that the function call uses HTMLInputElement as a generic:
image

The type checker we consume will therefore always report to us that the function's return type is the same as the type cast.

I'm going to close this, as there's nothing we can do about this from our side without adding a large amount of extra code to check if the value being cast comes from a function and that function has a generic etc.

As a workaround you can either delete the generic (I doubt you can, it's probably a library function, right?):

declare function foo(): Element;
const a = (foo() as HTMLInputElement).value;

Or use the generic.

declare function foo<T extends Element = Element>(): T;
const a = foo<HTMLInputElement>().value;

Both of these workarounds do not flag the lint rule, and type check okay.

If you feel strongly about it, I would suggest raising an issue in the typescript repo.

@vkrol
Copy link
Contributor Author

vkrol commented May 16, 2019

Thank you for the investigation.

Yes, it is the library function and I do no want to use the generic parameter because I think that it is the anti-pattern:

getMeAT(): T: If a type parameter does not appear in the types of any parameters, you don't really have a generic function, you just have a disguised type assertion. Prefer to use a real type assertion, e.g. getMeAT() as number. Example where a type parameter is acceptable: function id(value: T): T;. Example where it is not acceptable: function parseJson(json: string): T;. Exception: new Map<string, number>() is OK.

https://github.com/DefinitelyTyped/DefinitelyTyped#common-mistakes

I need to think a little more about it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants