Home > Software engineering >  Non null assertion operator not working in new Angular Library project?
Non null assertion operator not working in new Angular Library project?

Time:09-11

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?

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];

CodePudding user response:

Wrong place:

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

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

  • Related