Skip to content

[no-throw-literal] False positive for custom Error classes using generics (templates) #1427

@bajtos

Description

@bajtos

The recently introduced rule no-throw-literal (see #1331) does not recognize generic classes as Errors and report false positives.

Repro

module.exports = {
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  parserOptions: {
    sourceType: 'module',
    project: 'tsconfig.json',
  },
  rules: {
    '@typescript-eslint/no-throw-literal': 'error',
  },
};
class MyCustomError<Props extends object = {}> extends Error {
  constructor(extraProperties?: Props) {
    super('Panic!');

    Error.captureStackTrace(this, this.constructor);

    Object.assign(this, extraProperties);
  }
}

throw new MyCustomError();

See https://github.com/bajtos/typescript-eslint-sandbox/blob/master/src/no-throw-literal.ts

Expected Result

No problems are reported.

Actual Result

11:7  error  
  Expected an error object to be thrown  
  @typescript-eslint/no-throw-literal

Additional Info

When I remove the generic template argument and make MyCustomError class a regular class, then the rule works correctly and no error is reported.

Versions

package version
@typescript-eslint/eslint-plugin 2.15.0
@typescript-eslint/parser 2.15.0
TypeScript 3.7.4
ESLint 6.8.0
node 12.14.0
npm 6.13.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghas 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

      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