Home > Back-end >  How can i forbidden Non-null assertion operator in class properties?
How can i forbidden Non-null assertion operator in class properties?

Time:03-09

There is eslint role for no-non-null-assertion. My question is how can I disallow using non-null assertion in the class properties?

class A{
  someProp!:string
}

There is some typescript configuration or eslint role for that?

CodePudding user response:

Looking at the AST explorer, this production is called a PropertyDefinition, and adding the ! adds definite: true to the object. So, you can use the selector PropertyDefinition[definite=true] in combination with no-restricted-syntax.

rules: {
    'no-restricted-syntax': ['error',
        {
            selector: 'PropertyDefinition[definite=true]',
            message: 'Non-null property assertions are forbidden',
        },
    ],
},
  • Related