'TypeScript: Object is possibly 'null'. window.open
How does one make this work in Typescript?
window.open(externalUrl, '_blank').focus();
this throws the following typescript error:
Object is possibly 'null'.ts(2531)
I have tried this, but it does not work:
if (typeof window != 'undefined' && window && externalUrl !== '' && window.open(externalUrl, '_blank') ) {
window.open(externalUrl, '_blank').focus();
}
Solution 1:[1]
Based on @jcalz's excellent answer, there is also one-liner, using !:
window.open(externalUrl, '_blank')!.focus();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | T.Todua |
