-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Proposal: Compiler flag option to prevent assignment of any to a specifically typed prop #13539
Comments
the me, this is a worse problem: let a: any = "text"
let b: number
b = a // that's OK but I'd like not to be without explicit cast because I have a string where a number should be. |
In your editor, find all references on |
Well, no. I want the compiler to throw errors at me. This shouldn't be even possible without explicit cast. I do have some use cases of |
why not change these cases to |
Thanks. I'll do that after upgrade. However, that won't help with generic types probably. |
generic type inference never produces So the only two sources of |
Discussed at #24737; we don't have a good way forward on this. |
For anyone finding this now and wondering if there is a solution, you can use typescript-eslint with the following rules: "rules": {
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-return": "error"
} Links to the documentation for these rules can be found here: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules You can also add these rules, plus some others, using the "extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
] You must also add this to your eslint config to allow the eslint plugin find your ts project configuration which enables it to do type-checking: "parserOptions": {
"project": "./tsconfig.json"
}, Here's the full docs for the parser config: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
with a compiler option (that defaults to false), it would be good to prevent this
If enabled, this flag, would throw the same error for the assign from
sneak
as it does forcheater
Actual behavior:
It allows the assignment from the
any
type (as per the language spec), but defeating our type safetyI know that all types are sub types of
any
so not sure how feasible it would be, but in cases where I cant easily avoid a type being set to any (action.payload
coming from a middleware in my specific case. at least till we move to typed actions) it would be very helpful to be allowed to prevent this. And would still mean I could cast fromany
if I wanted to then move from anay
to a strongly typed objectThe text was updated successfully, but these errors were encountered: