Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rule proposal: Prevent / Warns if the code attempts to interpolate functions directly into string template #5600

Closed as not planned
@chenxinyanc

Description

@chenxinyanc

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

This rule prevents potential erroneous implicit Function to string conversion in string interpolation expressions. Converting a function (or class itself) into string will cause function body being inserted into the string template, which could be undesirable in most of the cases.

I think we can leverage TypeScript typing information for the "Function" type checking. We don't need to cover the case where the interpolated value does not have Function type at compile-time.

Fail Cases

const formatErrorMessage = (error: unknown) => String(error);
// `formatErrorMessage` is of function type. Converting it into string results in function body being printed.
const message = "Error is {formatErrorMessage}";

Pass Cases

const formatErrorMessage = (error: unknown) => String(Error);
// Call the function to return the string result.
const message1 = "Error is {formatErrorMessage(err)}.";
// Or if you are converting function into string by design.
const message2 = "Error is {String(formatErrorMessage)}.";

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existspackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions