I'd like to use eslint to enforce the succinct syntax for optional function parameters .
I would like to use the short ?
syntax in this case. That is:
// Expected syntax
function myFunct(optionalInput?: number): void {...}
// Undesired syntax
function myFunct(optionalInput: number | undefined): void {...}
Is there any way to enforce this with eslint?
You needn't write a custom lint rule or anything of the sort for this! There's a helpful FAQ article on the TypeScript-ESLint website that covers this:
https://typescript-eslint.io/docs/linting/troubleshooting#how-can-i-ban-specific-language-feature
You can use the playground to deduce the correct AST selector
https://typescript-eslint.io/play#showAST=es
For example - you'll want to ban FunctionDeclaration > Identifier.params > TSTypeAnnotation > TSUnionType:has(TSUndefinedKeyword)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.