0

.. I have a React app that I inherited that having problems figuring out a TS2345 “Argument of type __ is not assignable to parameter of type” error. One property that is in question from a Model assignment could be a factor

export class MyAppTheme{ 
  
    Themes?:MyAppTheme[];
}

I’m wondering if there is an issue with the Options operator ? on the MyAppTheme[] array

…but this block of code (in which I am providing the error info) comes up in various areas of the app, and I’m wondering if there is a typescript fundamental (I don’t hv much typescript experience) I’m missing, I’ve tried updating the npm packages the best of my knowledge >

Argument of type '(key: keyof MyAppTheme) => void' is not assignable to parameter of type '(value: string, index: number, array: string[]) => void'.
  Types of parameters 'key' and 'value' are incompatible.
    Type 'string' is not assignable to type 'keyof MyAppTheme '.  TS2345

     5 |         let theme = JSON.parse(JSON.stringify(sourceTheme)) as MyAppTheme;
     6 |         if(theme.Level == 0) {
  >  7 |             Object.keys(theme).forEach((key: keyof MyAppTheme) => {
       |                                        ^
     8 |                     if (key !== "Themes") {   
     9 |                         prodconfig[key] = theme[key];                
    10 |                     }

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
Thx Media
  • 1
  • 1
  • There's quite a bit of missing code here. What are the keys that are being iterated over? There's more than just the `Themes` array, right? Does this class have an array of all themes as well as a unique property for every theme? – Alex Wayne Jul 30 '21 at 19:13
  • Also, `MyAppTheme` has an instance property that is a an array of `MyAppTheme` instances, each of which has an instance property that is a an array of `MyAppTheme` instances, each which... and so on. Is this a tree structure of some sort? It's hard to answer you without more info. Please edit you question with some additional context. – Alex Wayne Jul 30 '21 at 19:25
  • 2
    This is probably a duplicate of [this question](https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript) but the OP doesn't realize it, I think. It would be nice if the code here were a [mcve] that others could test so I could be sure, though. – jcalz Jul 30 '21 at 19:26

0 Answers0