Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

Type 'undefined' cannot be used as an index type

$
0
0

I am trying to convert a function from Javascript to Typescript but I keep running into the following error:

Type 'undefined' cannot be used as an index type

I understand that undefined cannot be used as an index, but I have tried the null check for the index type as well as conditional rendering but nothing seems to work.

Here is my code:

useState hook of values I am using for the function

const [newUser, setNewUser] = useState({    firstName: "",    lastName: "",    companyName: "",    email: "",    password: "",    id: ""  });const [errorMessage, setErrorMessage] = useState("");

function which is essentially taking the newUser initial values, making a copy of that object, then calling setNewUser to populate the fields entered by the user into a newUser copied object.

const setValues = (propName?: number, value?: any) => {        const cloneUser = { ...newUser };        cloneUser[propName] = value;        setNewUser({ ... cloneUser });        setErrorMessage("");  }

My main issue is the cloneUser[propName] cannot be undefined and used as an index and I'm not sure how to get around this.


Viewing all articles
Browse latest Browse all 6290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>