简体   繁体   English

TSLint object-literal-sort-keys按字母顺序排序,但错误仍然存​​在

[英]TSLint object-literal-sort-keys is sorted alphabetically but error persists

TSLint is giveing me the warning The key 'allowedHeaders' is not sorted alphabetically (object-literal-sort-keys)tslint(1) to me this is alphabetically sorted yet tslint insists there is an error. TSLint向我发出警告The key 'allowedHeaders' is not sorted alphabetically (object-literal-sort-keys)tslint(1)对我来说是按字母顺序排序的,但tslint坚持存在错误。

I also dont know how to define the callback any correctly. 我也不知道如何正确定义回调。

What am I missing? 我想念什么?

// Configure CORS
const corsOptions = {
  origin: (origin: string, callback: any) => {
    if (process.env.CORS_WHITELIST && process.env.CORS_WHITELIST.indexOf(origin) !== -1) callback(null, true);
    else callback('Not allowed by CORS');
  },
  allowedHeaders: ['Accept', 'Authorization', 'Content-Length', 'Content-Type', 'X-Requested-With'],
  methods: ['DELETE', 'GET', 'OPTIONS', 'POST', 'PUT'], optionsSuccessStatus: 200,
};

It's about the keys of the literal corsOptions I think. 我想的是关于字面corsOptions的键的。 The key option should be placed at the end. 关键option应放在最后。

It's not about string values inside of allowedHeader , but about properties on corsOptions . 这不是内部的字符串值allowedHeader ,但有关性corsOptions Regarding callback function, possible definition is (string, boolean?) => any . 关于回调函数,可能的定义是(string, boolean?) => any

Here is the type with both corrections: 这是经过两种修正的类型:

const corsOptions = {
  allowedHeaders: ['Accept', 'Authorization', 'Content-Length', 'Content-Type', 'X-Requested-With'],
  methods: ['DELETE', 'GET', 'OPTIONS', 'POST', 'PUT'], optionsSuccessStatus: 200,
  origin: (origin: string, callback: (error: string, allowed?: boolean) => void) => {
    if (process.env.CORS_WHITELIST && process.env.CORS_WHITELIST.indexOf(origin) !== -1) callback(null, true);
    else callback('Not allowed by CORS');
  }
};

暂无
暂无

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

相关问题 object-literal-sort-keys 需要类型信息才能使用“match-declaration-order”或“match-declaration-order-only” - object-literal-sort-keys needs type info to use "match-declaration-order" or "match-declaration-order-only" TSLint - 防止错误:密钥不按字母顺序排序 - TSLint - Preventing error: The key is not sorted alphabetically 按字母顺序排序 TypeScript 接口键 - alphabetically sort TypeScript interface keys 将变量声明为 object 值时,TSlint 错误“object 文字中的预期属性简写......” - TSlint error “Expected property shorthand in object literal…” when declaring a variable to a object value tslint:“object-literal-shorthand”的好处 - tslint: benefit of "object-literal-shorthand" 为什么我在对象文字上出现错误类型断言被禁止,而是使用类型注释。 tslint(no-object-literal-type-assertion)? - Why I got an error type assertion on object literals is forbidden, use a type annotation instead.tslint(no-object-literal-type-assertion)? 为什么 tslint 会删除对象键? - Why does tslint remove object-keys? 访问对象中的可选键,其中键是文字联合类型中的文字,没有错误“对象可能是‘未定义’” - Access optional keys in object, where key is a literal in a literal union type without error "Object is possibly 'undefined'" 如何使用隐式键将对象文字声明为记录 - How to declare object literal as Record with implicit keys 使用对象文字的键作为 Typescript 类型? - Using the keys of an object literal as a Typescript type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM