8000 Rule proposal: Prefer `unknown` instead of `any` whenever possible · Issue #605 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
Rule proposal: Prefer unknown instead of any whenever possible #605
Closed
@sindresorhus

Description

@sindresorhus

I would like to enforce using unknown instead of any whenever possible for stricter typing.

Fail

function foo(x: any) {}

function foo(): any {}

Pass

function foo(x: unknown) {}

function foo(): unknown {}

Exceptions

There are a couple of exceptions:

  • For function arguments and assignment, {[key: string]: any} cannot be enforced into {[key: string]: unknown}, because it has special behavior:

    The index signature {[key: string]: any} in TypeScript behaves specially: it’s a valid assignment target for any object type. This is a special rule, since types with index signatures don’t normally produce this behavior.

  • For new(...args: any[]), I remember it not working when I tried to use unknown there.

There might be more exceptions I haven't thought of.


Because of the above exceptions, I cannot use the @typescript-eslint/ban-types rule for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulehas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0