简体   繁体   中英

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.

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. The key option should be placed at the end.

It's not about string values inside of allowedHeader , but about properties on corsOptions . Regarding callback function, possible definition is (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');
  }
};

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