Skip to content
Advertisement

typescript and reactjs : how to use map – ERROR ts(7053)

this is my first asked question in here, so please help me to improve.

In Typescript (ReactJs) given two arrays:

JavaScript

where MyType is a kind:

JavaScript

how can i print “value1” with the following code?

JavaScript

Right now I’am getting the following error:

JavaScript

Advertisement

Answer

You’re off to a good start, but there are a few things to fix!

Firstly, your first code snippet has incorrect types:

JavaScript

array1 isn’t a String, it’s an array of strings. So its type should be string[]. You’re also missing a quote after "value2:

JavaScript

Next, you have a syntax error in your console.log—it’s missing the ending ):

JavaScript

Then lastly @CertainPerformance’s answer can come in and save you: the type of array1 can be made more specific.

JavaScript

All together now:

JavaScript

Now, you asked about how to print value1. This will actually log ["value1", "value2"]. To log only the first one you can just access the first element after your .map():

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement