简体   繁体   中英

Typescript: Disabling es-lint rule for only an interface?

I have the rule in my project to disallow variables not in camelcase. I am hitting an external API where the response comes back with an object that has variables in camelcase. When I define a type:

interface AgeData {
    name: string,
    corner: number
}

interface APIResp {
    file_name: string,
    data_info: AgeData[],
    interest_m: number
}

I need to use camelcase for the expected format back from the API since I can't control the external API.
Throughout the rest of my code, I use:

// eslint-disable-next-line camelcase

to disable it but this doesn't seem to work for interfaces.
I don't want to change the rule or override it. It's a fairly large project and I just want it this one interface.

I don't think camelcase can be as type-aware as you're hoping for, but what it can do is:

  • Permit property names to have underscores (or forbid property names to have underscores). This is separate from permitting standalone identifiers from having underscores. Or
  • Permit specific whitelisted property names even if they don't match the pattern.

So, one way is to use the following config for the ESLint rule:

[
  'error',
  {
    allow: ['file_name', 'data_info', 'interest_m']
  }
]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM