0

I have a methodsfragment.ts file that is autogenerated for a graphql fragment query. I used the command npm run generate --graphql.

In the query we have a interface formElement which can be a input, select, radio etc based on the data. The data looks something like this:

    inputs[{
     formElements:{
        identifier: "first_name_input",
        label: "First name",
         placeholder: "" 
}
},{
formElement:{
  identifier: "select_year",
  label: "year",
  options:["1999","2000", "2001"]
}}]

in my methodsFragments.ts I have the definition of formElements something like:

export type inputRows_formElements = inputRows_formElements_firstNameInputField | inputRows_formElements_yearDropdown;

type  firstNameInputField {
 identifier: string!;
 label: string!;
 placeholder: string!;
}

type yearDropdown {
   identifier: string!;
   label: string!;
   options: string[];

}

when I am trying to access data, I do something like:

methods.map(formEle) =>{
  switch(formEle.identifier):
    case "select_year" :
      console.log("options length" + formEle.options); // at this line i get the error 
}

The error message is: "Property options does not exist on type inputRows_formElements".I am not sure how to resolve this error.

1 Answer 1

0

To resolve this error you use the as keyword provided in trypescript. So when you access the data for options you do something like:

methods.map(formEle) => {
  switch(formEle.identifier):
    case "select_year" :
      let curElement = formEle as inputRows_formElements_yearDropdown
      console.log("options length" + formEle.options); 
}

This should resolve this issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.