skip to Main Content

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.

enter image description here

.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:

enter image description here

2

Answers


  1. 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

    import fetch, { FormData } from 'node-fetch'
    
    const httpbin = 'https://httpbin.org/post'
    const formData = new FormData()
    
    formData.append('key[0]', 'Hello, world!')
    formData.append('key[0]', 'new name.txt')
    
    const response = await fetch(httpbin, { method: 'POST', body: formData })
    const data = await response.json()
    
    console.log(data)
    
    Login or Signup to reply.
  2. 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.

    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","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));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search