繁体   English   中英

Eslint & Typescript:将函数命名为 const 或 class 构造函数:如何转换?

[英]Eslint & Typescript: named functions as const or class constructor: how to transform?

我经常使用我要求的答案代码:

function sameValuesAsKeys<K extends string>(...values: K[]): {readonly [P in K]: P} {
  const ret = {} as {[P in K]: P}
  values.forEach(k => ret[k] = k);
  return ret;
}

随着时间的推移,来自 Angular 的 Eslint 现在抱怨 function。

Use const or class constructors instead of named functions

我试过了

const sameValuesAsKeys:<K extends string>=(...values: Array<K>): {readonly [P in K]: P} => {
  const ret = {} as {[P in K]: P};
  values.forEach(k => ret[k] = k);
  return ret;
}

它失败了,因为它找不到K和类似的方法

const TEST={
    sameValuesAsKeys(...values: Array<K>):<K extends string> => ({readonly [P in K]: P}) => {

也失败了。 任何提示如何正确格式化它以避免来自 ESlint 的错误?

这只是一个语法错误。 这编译:

const sameValuesAsKeys = <K extends string>(...values: Array<K>): {readonly [P in K]: P} => {
  const ret = {} as {[P in K]: P};
  values.forEach(k => ret[k] = k);
  return ret;
}

暂无
暂无

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

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