Search code examples
typescripteslinttypescript-eslint

Is there a linter for a missing typescript Type member?


I am trying to find a way to trigger a console lint error (or even a build error) when a Type member is called, but does not exist. Currently, I would have to know and open the offending file to see the error. This makes it easy to send bugs to production. Looking at eslint and typescript-eslint, there does not seem to be a rule for this. Anyone have a suggestion to add this check?

I am using the Vite build tool, as well as SonarCloud via Bitbucket.

eslint Config

module.exports = {
    root: true,
    env: { browser: true, es2020: true },
    extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react-hooks/recommended'],
    ignorePatterns: ['dist', '.eslintrc.cjs'],
    parser: '@typescript-eslint/parser',
    plugins: ['react-refresh'],
    rules: {
        'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
        '@typescript-eslint/no-explicit-any': 'warn'
    }
}

Problem

export enum ExampleItem {
   Item1: 'Item 1',
   Item2: 'Item 2',
   Item3: 'Item 3' //was deleted from file without a proper regression test
}

const item3 = ExampleItem.Item3; //does not exist, how to error this in console?

'@typescript-eslint/no-unsafe-assignment' fake-picks up the error. But adding /* eslint-disable @typescript-eslint/no-unsafe-assignment */ disables that specific error in the file and still leaves behind the missing Type member without flagging it.

And, this may really be asking to find a linter for any kind of missing object member.


Solution

  • Eslint does not flag this as an error (because it doesn't verify types), but TypeScript's type-checker (tsc) will.

    export enum ExampleItem {
       Item1 = 'Item 1',
       Item2 = 'Item 2',
    }
    
    const item3 = ExampleItem.Item3; // Error: Property 'Item3' does not exist on type 'typeof ExampleItem'. 
    

    Playground