Skip to content

no-unsafe-call: ban calling Function #9108

Closed
@kirkwaiblinger

Description

@kirkwaiblinger

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Unintuitively, TS narrows typeof x === 'function' to the unsafe Function type if x is not already constrained. However, it is safe to use on a constrained type. We should report on unconstrained callables.

Fail Cases

declare const maybeFunction: unknown;
if (typeof maybeFunction === 'function') {
    // TS allows this.
    maybeFunction('call', 'with', 'any', 'args');
}

Pass Cases

declare const maybeFunction: string | (() => string);
if (typeof maybeFunction === 'function') {
    // TS errors if more args are provided than the declared call signature, so this is safe.
    maybeFunction();
}

Additional Info

it's probably fine/good to allow typeof x === 'function as long as x isn't called. I'm not sure exactly where the line should be drawn between reporting and not reporting. It seems like it should be reported if we pass x to a function that might expect a specific call signature. Maybe a heuristic could be typeof x === 'function is ok as long as x isn't referenced in the true branch of the conditional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: 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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy