Type 'SafeResourceUrl' is not assignable to type 'string'

As already suggested, you can just remove the variable declaration (or declare any type), but I doubt many would agree to be the correct solution.

The various Dom sanitizer methods don't return strings, they return various object types.

See the official API docs: https://angular.io/api/platform-browser/DomSanitizer

this.sanitizer.bypassSecurityTrustResourceUrl(url);

Returns a SafeResourceUrl type object, not a string; so your declaration should reflect this rather than the nebulous any type.


I got a way to fix this. I tried sanitizing the SafeResourceUrl again with sanitize() method whose return value is string | null.

In case you want to use bypassSecurityTrustUrl(), then SecurityContext.URL will take place. In my case I used SecurityContext.RESOURCE_URL


export class LandingpageComponent implements OnInit {
     public pdfSrc: string;
}

constructor(    
    private sanitizer: DomSanitizer) {
}

fnOpenAsset() {     
   let url = 'http://localhost/pdf_viewer-master/18/docs/pdf.pdf';
   this.pdfSrc = this.sanitizer.sanitize(SecurityContext.RESOURCE_URL, this.sanitizer.bypassSecurityTrustResourceUrl(url));
}