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

Proposal: Compiler flag option to prevent assignment of any to a specifically typed prop #13539

Closed
bakesteve opened this issue Jan 17, 2017 · 9 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@bakesteve
Copy link

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
let test = 'Hello';
let sneak: any = 4;
let cheater = 4;
test = cheater; // not allowed
test = sneak; // OK

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 for cheater

Actual behavior:
It allows the assignment from the any type (as per the language spec), but defeating our type safety

I 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 from any if I wanted to then move from a nay to a strongly typed object

@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2017

This is what any means really. may be what you are looking for is something like #13316 (and subsequently #13473) to allow you to remove all explicit any declarations?

@Namek
Copy link

Namek commented Mar 20, 2017

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.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Mar 20, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Mar 20, 2017

In your editor, find all references on any, this should list all the ones you have in your project.

@Namek
Copy link

Namek commented Mar 21, 2017

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 any that I can't change.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 21, 2017

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 any that I can't change.

why not change these cases to object? this will force you to cast when use them.

@Namek
Copy link

Namek commented Mar 23, 2017

Thanks. I'll do that after upgrade.

However, that won't help with generic types probably.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 23, 2017

However, that won't help with generic types probably.

generic type inference never produces any if it was not already there. if inference found no candidates it defaults to the upper bound (i.e. {}).

So the only two sources of any in your compilation are 1. implicit any for un-annotated declarations (should be all flagged with --noImplicitAny as errors), and 2. explicit any annotations (in your code, or references .d.ts files), these should be detectable by find-all-references in your IDE with TS 2.2 and later.

@RyanCavanaugh
Copy link
Member

Discussed at #24737; we don't have a good way forward on this.

@stefee
Copy link

stefee commented Sep 5, 2021

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 recommended-requiring-type-checking preset:

  "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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants