0

I'm getting an error on the fields property when using Typescript from this tutorial

class Profile extends React.Component<Props, State> {
  constructor(props: any){
    super(props);
  }
  render(){
    const {fields} = this.props;
    return(
    <div>
      <p>Profile Component</p>
      <Text field={fields.heading} />
    </div>
    )
  }
}

The Typescript compiler gives the following error:

[ts] Type 'string' has no properties in common with type '{ value?: string; editable?: string; }'. [2559]
Text.d.ts(4, 5): The expected type comes from property 'field' which is declared here on type 'IntrinsicAttributes & TextProps & { children?: ReactNode; }'
(JSX attribute) TextProps.field: {
    value?: string;
    editable?: string;
}
The text field data.

Which is actually in line with the Text.d.ts from JSS:

/** The text field data. */
field: {
    value?: string;
    editable?: string;
};

When I actually use the format that typescript wants <Text field={{value: fields.heading}} /> I get the following error:

Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead.

Updating the Text.d.ts to field: string; actually works, but that's not the typing specified by JSS.

0

The interface used in the example was off, the interface for fields should be an object with value.

interface IFields {
  heading: {
      value?: string;
  };
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.