1

I have seen this issue explained from Bobby Hadz for arrays of the same type, but I cannot find anythinjg about objects that contain different types.

Here is the error that is produced. Type 'string | number | boolean' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'.

Here is the code that causes the TS error.

const geometryCopy : geometryTypes  = {... geometryValues}
//Append the new value to the object
if(geometryCopy?.[attributeName]) geometryCopy[attributeName] = newValue

attributeName has its type as keyof geometryType.

Any help would be greatly appreciated on how to resolve the issue or any best practices that I am not following.

Edit: I have recreated the error on this ts playground.

  • You have a syntax error combining **dot notation** and **bracket notation**: `geometryCopy?.[attributeName]`. I cannot reproduce your question, can you reproduce at this [ts playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBA5hD2BbCwBOIAq4IGcoF4oBvAQ2DQEYAuKHNASwDsYAaKMtAJhsYFdEARhFQBfAFBiAxvEZ1YCZGhAA1EgBteuGnCQp0WSHkKlyqagCI6qc2w6puUCuKky5OxegDC8MCCjaFPUxsPAJiADpI+V0lVQ1cZ3oAMwAKdyDvXwBtczsKcwBdAEpojxBMkBy8wrDzRggAdzjNcyA)? BTW the blog describe mutating array, but your's are object. – Enfield Li Aug 10 '22 at 09:55
  • 1
    @Enfieldli - it is valid syntax. See https://stackoverflow.com/questions/58780817/using-optional-chaining-operator-for-object-property-access – Tobias S. Aug 10 '22 at 10:21
  • 1
    @Enfieldli it's totally valid syntax (optional chaining), it's not an error. – Danila Aug 10 '22 at 10:21
  • @Danila Yeah, you got it, I wasn't aware of it – Enfield Li Aug 10 '22 at 10:23
  • @Enfieldli I have recreated the issue and added it to the end of the original question. – QuirkyGoodFriend Aug 10 '22 at 13:46

0 Answers0