简体   繁体   English

打字稿:仅对接口禁用 es-lint 规则?

[英]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.我正在访问一个外部 API,其中响应返回一个对象,该对象具有驼峰形式的变量。 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.由于我无法控制外部 API,因此我需要使用驼峰式从 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:我不认为camelcase可以像您希望的那样具有类型意识,但它可以做的是:

  • 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:因此,一种方法是对 ESLint 规则使用以下配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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