Property 'label' is missing in type 'string[]' but required in type - typescript error

Viewed 32

I have a type called Person like this :

type Person = {
  value?: string[];
  label?: string[];
};

I have this promise function

async function main(): Promise<Person> {
  const foo = await dynamicsWebApi.retrieveAll("accounts",["name"]).then(function (stuff) {
    var records = stuff.value
    
    const options = records?.map(d => ({
      "value" : d.name,
      "label" : d.name

    }))
    console.log(options)
    
    
  }).catch(function (error) {
      console.log(error)
  })
  
  return options
}

So I transform the api call into value and label (it is called options) and I need to pass this to a react component (in my return statement.

but I am getting the error

Type 'string[]' has no properties in common with type 'Person'

when I hover over the 'return options' command .

Any idea how I can craft my promise better to avoid this error ?

Thanks !

1 Answers

Well, it seems strange to me like you're retrieving a list of person objects, which each have 1 label and 1 value. This is not consistent with the Person object you defined.

If this is the case, this could be written like this:

type Person = {
  value?: string;
  label?: string;
}

async function main(): Promise<Person> {
  const stuff = await dynamicsWebApi.retrieveAll("accounts",["name"]);
  const records = stuff.value;

  if (!records) {
    return [];
  }

  const options: Person[] = records.map(d => ({
      value: d.name,
      label: d.name
    }));
  
  return options;
}
Related