Skip to content
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

Turn on no-explicit-any #2115

Open
ShadowJonathan opened this issue Jan 23, 2022 · 2 comments
Open

Turn on no-explicit-any #2115

ShadowJonathan opened this issue Jan 23, 2022 · 2 comments
Labels
T-Task Tasks for the team like planning

Comments

@ShadowJonathan
Copy link
Contributor

ShadowJonathan commented Jan 23, 2022

https://github.com/matrix-org/matrix-react-sdk/blob/010cbadc8ecf9f15ade94d87950734c873ca6095/.eslintrc.js#L81-L82;

            // We disable this while we're transitioning
            "@typescript-eslint/no-explicit-any": "off",
@ShadowJonathan ShadowJonathan changed the title Turn off no-explicit-any Turn on no-explicit-any Jan 23, 2022
@SimonBrandner SimonBrandner added the T-Task Tasks for the team like planning label Jan 23, 2022
@turt2live
Copy link
Member

It'd be good to have rationale for why this change is important to you.

@ShadowJonathan
Copy link
Contributor Author

As I've explained in #2116, it is weird to me that some lints are disabled, as without them, breaking behaviour would be allowed that undermines the basis of TS' type-checking system.

any is an example of this, as it is TS saying "i don't know" in the sense that it does not properly understand a type-check inference that is taking place. Often these are types imported from outside libraries, but this can also include unknown data, such as JSON-parsed strings.

any propagates any, and it is not properly checked without lints such as #2120.

any is also assignable to anything, the following is allowed, and afaik, there are no lints forbidding it;

let a: any = {};
let b: number = a;
// now b is typed as a number without errors

There is an alternative, unknown, is is described as a "type-safe variant" of any, only being assignable to itself. Example, the following errors out;

let x: unknown = {};
let y: number = x;
// y errors with "unknown is not assignable to number"

So, with these alternatives, eradicating any from the codebase, and instead having every such instance (replaced by unknown) as dealing with truly unknowable data, could make code more safe and deterministic. Data returned from an API would have to be type-guarded (by checking properties and their types), or type-casted, before they're assignable to new types.

To me, this type of change seems obvious, as the alternative would be that behaviour surrounding any would be unchecked, it would break the basics of TS' type safety, creating a false sense of security, and leading to headaches, programmer bugs, runtime nullability problems, and at worst, vulnerabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Task Tasks for the team like planning
Projects
None yet
Development

No branches or pull requests

3 participants