Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Non null assertion operator not working in new Angular Library project?

I created a new Angular library project with this code.

/**
 * Extract the path from the URL.
 *
 * @param url The URL to extract the path from
 */
 export function urlPath(url: string): string {
    return url!.match(/.*?(?=[?;#]|$)/)[0];
}

And even though I have a non null assertion operator ! appending url I still get lint errors saying that url might be null ( Object is possibly ‘null’.ts(2531) ).

Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the output from the build:

✖ Compiling with Angular sources in Ivy partial compilation mode.
projects/fs-validatorts/src/lib/utilities/url/urlPath.ts:7:12 - error TS2531: Object is possibly 'null'.

7     return url!.match(/.*?(?=[?;#]|$)/)[0];

>Solution :

Wrong place:

return url.match(/.*?(?=[?;#]|$)/)![0];

You want to assert that there will always be a match.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading