I have added TS to my React/Redux app.
I use window
object in my app like this:
componentDidMount() {
let FB = window.FB;
}
TS throws an error:
TypeScript error: Property 'FB' does not exist on type 'Window'. TS2339
I want to fix the error.
1 (doesn't work)
// Why doesn't this work? I have defined a type locally
type Window = {
FB: any
}
componentDidMount() {
let FB = window.FB;
}
// TypeScript error: Property 'FB' does not exist on type 'Window'. TS2339
2 (fixes the error)
I found the answer here https://mcmap.net/q/45448/-how-do-you-explicitly-set-a-new-property-on-window-in-typescript
declare const window: any;
componentDidMount() {
let FB = window.FB;
}
// No errors, works well
Why doesn't the first version work, but the second does, even though I do not specify FB property at all?