Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructuring assignment via TypeScript in React

How should I do something like

class App extends React.Component {
    render() {
      const { x, y, z} = this.props;
      return (...)
    }
}

in TypeScript? For now tslinter shows an Error:

Type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' has no property 'x' and no string index signature.

like image 841
alex 9835780012 Avatar asked Apr 29 '26 12:04

alex 9835780012


1 Answers

You have to define a type for props:

interface Props {
    x: string;
    y: number;
    z: string;
}

class App extends React.Component<Props, {}> {
    render() {
      const { x, y, z} = this.props;
      return (...)
    }
}
like image 139
Bsalex Avatar answered May 02 '26 04:05

Bsalex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!