I’m using Typescript and ESLint in my project and I am stuck on a problem with the error: An object literal cannot have multiple properties with the same name.
I just want to disable checking for this error in the line but I don’t know how to do it.
This looks like the no-dupe-keys
rule from the docs but for some reason it doesn’t work in my project.
testFetch.ts
import fetch from 'node-fetch';
export function testRequest() {
const url:string = 'https://example.com/api';
// eslint-disable-next-line camelcase
const test_camel = 'sd'
const body = {
"key[0]": "arr_val_1",
// eslint-disable-next-line no-dupe-keys
"key[0]": "arr_val_2",
// eslint-disable-next-line camelcase
"key[1]": test_camel,
}
fetch(url, {body})
.then((res) => res.json())
.then((res) => console.log(res))
.catch((err) => console.error('error:' + err));
}
I know… but such weird body params are required.
As you can see on the screenshot linter highlights it anyway.
.eslitrc.cjs
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
rules: {
"no-dupe-keys": "error",
camelcase: "error",
},
ignorePatterns: [
"test/*",
"dist/*",
".eslintrc.cjs"
],
// "noInlineConfig": true,
};
Works perfeclty in the playground but not in the real project:
2
Answers
It’s IMPOSSIBLE to have an object in javascript with two identical keys. So no option of eslint or typescript will give you what you are trying to do.
The API probably requires a form-data like structure.
An example from https://www.npmjs.com/package/node-fetch#formdata
In JSON, key should be unique you can’t send 2 keys with the same name.
I’m guessing you want to send 2 values for the
key[0]
as query params to the backend.For that, you can send an array in query params as well.
In the backend get the value of the key
key[0]
and traverse it as an array.