Aliaksandr Kirylau
Aliaksandr Kirylau

Reputation: 192

Destruct react props using Typescript

I want to desctruct react props that I pass to component with Typescript. These are my props:

export type IssueListHomeProps = {
    projects: ProjectDtoImpl[];
    issues: IssueDtoImpl[];
    timeEntries: TimeEntryDtoImpl[];
    handleRefresh: () => void;
    changeMode: (m: string) => void;
    selectTimeEntry: (entry: TimeEntryDtoImpl) => void;
    pullToRefresh: boolean;
    dates: string[];
} & RouteComponentProps;

That's how I'm trying to do this:

const {projects: ProjectDtoImpl[], issues: IssueDtoImpl[],timeEntries: TimeEntryDtoImpl[],pullToRefresh: boolean, dates: string[]} = this.props

But I get errors like these:

1.Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
2.Expression expected

I don't know where to do this: in component or outside, or may be in the constructor? Thanks in Advance!!

Upvotes: 1

Views: 1014

Answers (1)

Aliaksandr Kirylau
Aliaksandr Kirylau

Reputation: 192

Well, It is incorrect to use types in destruction; The correct way is to make it like this:

render() {
    const {
      projects,
      issues,
      timeEntries,
      pullToRefresh,
      dates
    }: IssueListHomeProps = this.props;

Upvotes: 2

Related Questions