Skip to content

[restrict-template-expressions] errors even when class defines toString() #3538

Closed
@nicholaschiang

Description

@nicholaschiang
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

// .eslintrc.js
const path = require('path');

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint', 'promise'],
  parserOptions: { project: [path.resolve(__dirname, 'tsconfig.json')] },
  extends: [
    'next',
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:promise/recommended',
    'prettier',
  ],
  env: {
    browser: true,
    node: true,
  },
};
// lib/message.ts
class Message {
  public toString(): string {
    return 'I am a custom toString() method';
  }
}

const message = new Message();
console.log(`This is the message: ${message}`); // results in an error
console.log(`This is the message: ${message.toString()}`); // no error

That ☝️ creates an error saying:

Invalid type "Message" of template literal expression.
// tsconfig.json
{
  "$schema": "http://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "downlevelIteration": true,
    "baseUrl": "."
  },
  "exclude": ["node_modules"],
  "include": [
    "next-env.d.ts",
    "types/*.d.ts",
    "lib/**/*.ts",
    "lib/**/*.tsx",
    "components/**/*.ts",
    "components/**/*.tsx",
    "pages/**/*.ts",
    "pages/**/*.tsx",
    "styles/**/*.ts"
  ]
}

Expected Result

There should be no error because the no-base-to-string rule should apply here; ESLint should realize that I've defined a custom toString() method on Message and thus Message is a valid type to be in a template expression.

Actual Result

I got this error from ESLint:

Invalid type "Message" of template literal expression.

Versions

package version
@typescript-eslint/eslint-plugin 4.27.0
@typescript-eslint/parser 4.27.0
TypeScript 4.3.2
ESLint 7.28.0
node 12.8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions