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

91
Visualizações
Typescript assertion function for manipulating objects

I want to manipulate an object with a function that parses all given keys to a number. In addition, the function should create the correct type for the object.

This is what I have until now:

type ReplaceStringWithNumber<T> = T extends string ? number : T

type ReplaceStringsWithNumbers<T extends Record<PropertyKey, unknown>, K extends keyof T> = {
  [P in keyof T]: P extends K ? ReplaceStringWithNumber<T[P]> : T[P]
}

export function parseIntIfSet<T extends Record<string, unknown>, K extends keyof T>(
  object: T | ReplaceStringsWithNumbers<T, K>,
  key: K
): asserts object is ReplaceStringsWithNumbers<T, K> {
  if (object[key]) {
    object[key] = Number(object[key]) as T[K]
  }
}

It looks good for now but when I test the function:

const p = {id: '1'}
parseIntIfSet(p, 'id')
// `p.id` is of type `never`, because `p` is of type `{id: string} & {id: number}`

Does anyone know how to solve this? A solution which works is to return a new object with the manipulated type, but this is not exactly what I want.

I am also not sure, why I need to use T | ReplaceStringsWithNumbers<T, K> in the parameters as I get an error if I just use T.

about 4 years ago · Juan Pablo Isaza
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