Joelgullander
Joelgullander

Reputation: 1684

index expression is not of type

I am getting the following error on window['SERVER_DATA']

Element implicitly has an 'any' type because index expression is not of type 'number'

const initialState = Object.assign(window['SERVER_DATA'] || {}, {
  auth: authState
});

Anyone got any idea on how to fix this?

Upvotes: 0

Views: 90

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249696

window does not have a property named SERVER_DATA do you can't use the string 'SERVER_DATA' to index window (window does have a numeric index, hence the error).

You can add the property using augmentation:


declare global { // this line is necessary only if you are a module
    interface Window {
        SERVER_DATA: unknown 
    }
}

Or you can cast window to something indexable with a string: (window as Record<string, unknown>)['SERVER_DATA'] || {}

You can use a more specific type instead of unknown

Upvotes: 1

Related Questions