I am using something like this using RegEx.
const body = /<body.*?>([\s\S]*)<\/body>/.exec(html)[1];
Expected: Should compile without error.
Actual: [ts] Object is possibly 'null'.
Help me to get of this...
I am able to solve this question using non-null assertion operator !
as below
const body = /<body.*?>([\s\S]*)<\/body>/.exec(html)![1];
If you don't want to use the !
operator, one other option could be to use the optional operator ?
and use a default value.
const body = /<body.*?>([\s\S]*)<\/body>/.exec(html)?[1] ?? '';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With