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

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

TSLint 向我发出警告 The key 'allowedHeaders' is not sorted alphabetically (object-literal-sort-keys)tslint(1) 这是按字母顺序排序的,但 tslint 坚持认为有错误。

我也不知道如何正确定义回调。

我错过了什么?

// 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,
};

我认为是关于文字 corsOptions 的键。关键option应该放在最后。

这与 allowedHeader 内的字符串值无关,但与 corsOptions 上的属性有关。关于回调函数,可能的定义是(string, boolean?) => any.

这是两种更正的类型:

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');
  }
};