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
[no-unsafe-assignment] fails to infer the type from class properties #2109
Comments
Yeah I ran into this when attempting to introduce the rule into this codebase. For some reason TS doesn't automatically infer the type of the RHS if it's I think it's due to how the interface MapConstructor {
new(): Map<any, any>;
new<K, V>(entries?: readonly (readonly [K, V])[] | null): Map<K, V>;
readonly prototype: Map<any, any>;
}
declare var Map: MapConstructor; Compared to interface SetConstructor {
new <T = any>(values?: readonly T[] | null): Set<T>;
readonly prototype: Set<any>;
}
declare var Set: SetConstructor; Note that Unsure if it's a "bug" in TS or not. Either way, it's something that will have to be special cased in the rule unfortunately. |
Doing some research, it looks like this is intentional. However because So the map constructor being like this is completely intentional, which means we'll have to add a special case for |
Thanks for the quick update. Sounds like this is quite the rabbit hole! I've sadly found I have had to disable all of the |
How so? If there are problems with the rule, please report them so we can look into getting them fixed! |
I ran into this issues today as well. Would be lovely if If I implement my own custom Generic Fixing TypeScript standard library for ES6 / etc to remove |
Here's an example workaround for JSDoc. class Foo {
constructor () {
// https://github.com/typescript-eslint/typescript-eslint/issues/1943
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/** @type {Map<string, FunctionConfiguration[]>} */
this._profiles = /** @type {Map<string, FunctionConfiguration[]>} */ (new Map())
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
}
} JSdoc does not support |
… map constructor with no generics Fixes #2109 Sucks that it had to be this way, but oh well.
Repro
Expected Result
The
no-unsafe-assignment
rule is flagging the creation of the Map in the constructor as being unsafe, claiming it can acceptany
types, even though the type is enforced by theevents
property in the class.Actual Result
I see no logical reason why this should be considered an unsafe assignment when the type is clearly enforced by the property. TypeScript itself prevents incorrect insertion of values into this Map.
Additional Info
In order to stop the rule complaining, the following works:
However, this is just duplicating the type already defined.
Versions
@typescript-eslint/eslint-plugin
3.0.1
@typescript-eslint/parser
3.0.1
TypeScript
3.9.3
ESLint
7.1.0
node
10.16.0
npm
6.9.0
The text was updated successfully, but these errors were encountered: