I recently started using Typescript
with Nextjs and encountered an error while trying to typecheck a basic component. How can I resolve the error and typecheck my array of objects?
ERROR in C:/Users/Matt/sites/shell/pages/index.tsx(22,4):
22:4 Property 'swc' does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'.
20 | render() {
21 | const {
22 | swc: { results }
| ^
23 | } = this.props;
type Props = {
results: Array<any>;
};
Component:
interface Props {
swc: Object;
results: Array<any>;
}
class swc extends React.Component<Props, {}> {
static async getInitialProps() {
const res = await fetch("https://swapi.co/api/people/");
const swc = await res.json();
return {
swc
};
}
render() {
const {
swc: { results }
} = this.props;
return results.map(swc => (
<Layout>
...
</Layout>
));
}
}
export default swc;
this.props:
{ swc:
{ count: 87,
next: 'https://swapi.co/api/people/?page=2',
previous: null,
results:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] },
url:
{ query: [Getter],
pathname: [Getter],
asPath: [Getter],
back: [Function: back],
push: [Function: push],
pushTo: [Function: pushTo],
replace: [Function: replace],
replaceTo: [Function: replaceTo] } }