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://stackoverflow.com/a/56402425/1114926
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?