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

304
Visualizações
Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict'

I am trying to replace an object using SDK, and typescript throws the following error with the 'strict' mode on,

const offer = client.offer(oldOfferDefinition!.id);
await offer.replace(newOfferDefinition);

error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.

const offer = client.offer(oldOfferDefinition!.id);

I am sure that oldOfferDefinition has id and it has a value, how can i get rid of this compliation error?

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

0

If you are sure that it does, then throw an exception if it doesn't.

const id = oldOfferDefinition!.id;
if (typeof id === "undefined") {
    throw new Error("ID for old offer is undefined");
}
const offer = client.offer(id);

Since the runtime can't reach the line where you pass the value to offer if that value is undefined, then the TS compiler will be happy with it.

about 4 years ago · Juan Pablo Isaza Relatório

0

remove ! mark form oldOfferDefinition, and encapsulte this to condition

if(oldOfferDefinition){
    const offer: string = client.offer(oldOfferDefinition.id);
    await offer.replace(newOfferDefinition);
}

but you should secure, that the result type of offer() function is defined as string

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