Elena Semenova
Elena Semenova

Reputation: 141

Spread operator error on react and typescript

I have a function:

handleMarkerClick(targetMarker) {
    this.setState({
        markers: this.state.markers.map(marker => {
            if (marker === targetMarker) {
                return {
                    ...marker, //  error TS1136: Property assignment expected.

                    showInfo: true, // error TS1005: ',' expected.
                }; // error TS1135: Argument expression expected, error TS1005: ')' expected.
            }
            return marker; //  error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
        }), // Declaration or statement expected.
    });
}

and I see many errors:

But when I delete '...' near 'marker' my function wasn't work correct.

I do this:

    handleMarkerClick(targetMarker) {
    this.setState({
        markers: this.state.markers.map(marker => {
            if (marker === targetMarker) {
                return {
                    marker, // delete ...
                    showInfo: true,
                };
            }
            return marker;
        }),
    });
}

Why? What I can do to get right function?

Upvotes: 3

Views: 2779

Answers (1)

vlence
vlence

Reputation: 495

If marker is an object then instead of using the spread operator you can try using Object.assign(). Here's an example:

marker = Object.assign({}, marker);

Upvotes: 2

Related Questions