Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda