Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
How to convert a JS k-v object to a typescript version?

I want to convert a js object into typescript version (we are refactoring the code) :( But I don't get how to migrate a JS JSON object into a correct TS format. For example, we have a JS object like this:

JS Object:
{
  "filter": {
    "price": {
      "$gte": 10,
      "$lte": 100
    },
    "symbol": "appl"
  },
  "sort": {
    "createdAt": -1
  }
}

So it's easy in js code that we declare a params = {} and we can insert params.filter[price] = .....

However, if we want to do it in Typescript, the compiler will complain that we need to determine the type, and it's hard because as you can see the "value" can be string or int or another object.

Do you have any ideas on it? Super thanks!!!!!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There is no need to create a type/interface in order to parse JSON:

const json = `{
  "filter": {
    "price": {
      "$gte": 10,
      "$lte": 100
    },
    "symbol": "appl"
  },
  "sort": {
    "createdAt": -1
  }
}`

const data = JSON.parse(json)

console.log(data)

If you want too, you can create an interface and assign the parsed data to it. Note however that this doesn't validate that the parsed data matches the type/interface (it could be any arbitrary JSON data and it would be assigned to the result variable, but typescript would then only let you access the members that exist in the interface.

interface Data {
  filter: {
    price: {
      $gte: number
      $lte: number
    }
    symbol: string
  }
  sort: {
    createdAt: number
  }
}

const json = `{
  "filter": {
    "price": {
      "$gte": 10,
      "$lte": 100
    },
    "symbol": "appl"
  },
  "sort": {
    "createdAt": -1
  }
}`

const data: Data = JSON.parse(json)

console.log(data)
console.log(data.filter.symbol)
about 4 years ago · Juan Pablo Isaza Relatório

0

If you want type of specific object try using VSCode Paste JSON as Code extension or this website that will generate typescript interface from your object http://json2ts.com/ .

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use TypeScript interfaces to do so. You can easily generate TS using websites or extensions like https://app.quicktype.io/

TS

export interface Pokedex {
    filter: Filter;
    sort:   Sort;
}

export interface Filter {
    price:  { [key: string]: number };
    symbol: string;
}

export interface Sort {
    createdAt: number;
}

JS

{
  "filter": {
    "price": {
      "$gte": 10,
      "$lte": 100
    },
    "symbol": "appl"
  },
  "sort": {
    "createdAt": -1
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda