3

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.

1 Answers1

9

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 (...)
    }
}
Bsalex
  • 2,847
  • 1
  • 15
  • 22
  • 1
    And for components without state it's safer to pass `never` instead of `{}` as the second type assertion. – inkdeep Feb 08 '18 at 17:11