Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!