-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New set of rules: no-unsafe-*
#791
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
Comments
Will this name be confused with the tsconfig.json setting The spirit is similar, but the meaning is different, and it might be confusing in conversation. "Hey Pete, I tried what you said but I keep getting no implicit any errors!" [much puzzlement later] "Ohhhhhhhh you meant the lint rule!" Maybe |
Definitely a valid concern. We can look at naming it something different. This was the first thing that came to my mind because of concepts of "implicitly returning any", "implicitly typing a variable as any", etc.
|
In tslint this was called |
Having a quick look at the tslint docs, it doesn't seem like it's a 1:1 - it looks like the tslint rule only handle usage of a variable of type But it's definitely a valid idea for a name, as long as we're clear that it's not a 1:1 replacement for the tslint rule. (as an aside, I hate that the tslint docs don't include examples.. I'm always left wondering exactly what their rules cover). |
|
|
I think the simplest description is that |
Yeah, I wish the TypeScript compiler would just have an option to treat |
Seems like the TypeScript maintainers already decided not to 😢 |
Related: |
I would love to have this rule as this is a big blind spot in type safety of large codebases. One specific use case I have a lot is that caught exceptions in TypeScript are of type Even if TypeScript did this for lib.d.ts (which is very difficult with BC), it would still exist in many typings of libraries, so a rule is very useful. |
Would this also cause an error?
I would hope so, even though it is not an implicit |
Hard to say - using an explicit cast might be a way to say the rule should ignore this expression. And any usages of the return value would be flagged by this rule: const bar = (something as any).foo();
bar.baz // no-implicit-any error - accessing property on any type
function baz() {
return (something as any).foo(); // no-implicit-any error - returning any type
}
// etc |
Hi, nothing has been defined so far? Is there any palative solution until topic is resolved? |
https://github.com/plantain-00/type-coverage is useful for spotting |
This would be handy. We made the switch from tslint to eslint to only find this unavailable :(. Now we have bugs creeping into our codebase due to unsafe usages of any. Although we try to make people remember to use |
@k290 in the mean time, i use the tslint plugin for eslint to run only this exact rule together with my eslint rules. |
@k290 have you looked at our Also there's our I get that |
@bradzacher, for me the biggest risk is typically when I'm using FP style, piping/composing functions (i.e., using lodash's flow/pipe functions). It's often not quite clear that one or more of the results being passed to the next function is actually I've resorted to @fabb's approach of turning on VS Code's tslint extension solely for its Speaking to @danielnixon's suggestion of using |
You should instead try our eslint plugin,
It's all open source. Features are just a PR away! |
@bradzacher, I didn't know about I really wish I had the time to dive into a type coverage feature... I guess that's the other side of that OSS coin -- projects are always at the mercy of (potential) contributors' schedule demands. |
Thanks @bradzacher , the rules you posted further up will help us. |
The approach to have one rule for each possible expression is interesting, but I feel like there's going to be a large number of expressions not covered. Excluding the rules already added in above PRs:
Should/can we really have a rule for every single one for these cases? |
I think most, if not all projects use Note also that arrays do not have the same check applied to them 😢. const a = [1,2,3];
const b = a[1 as any]; // no ts error
// ^^^^^^^^ no-unsafe-member-expression
const x = { prop: 1 };
const y = x[1 as any]; // Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ prop: number; }'.
// ^^^^^^^^ no-unsafe-member-expression
This is covered by
At this point, this should be implicitly handled by any of the previous rules.
|
(reopening for the aforementioned missing cases and extra rules) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Note from investigation - I realise I'm very wrong. Typescript knows that it doesn't matter what the type of the operands for most binary expressions ( So a new rule, Also for clarity as I merge #1968 in here. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Fixes #791 This adds a new rule which just checks the passing of arguments. This case is a lot more complex than most cases due to spread arguments.
These rules are all implemented - one final case remains to be implemented:
no-unsafe-argument
to catch when you're passingany
into an argument. (See #791 (comment))Create a rule which uses type information to determine when you're inadvertently breaking type safety by using an any, potentially without knowing.
Often libraries (or even the typescript defs themselves) can have weak(/lazy) types which return
any
. If you're not careful, you can inadvertently introduceany
s within your codebase, leading to bugs that aren't caught by the compiler.Examples:
Should also check assignments:
Maybe also check things boolean things like if/while/etc?
Should also provide an option to go strict with property access to help with finding the source of an any via property access):
The text was updated successfully, but these errors were encountered: