I want to use an external library in my typescript application, but I also want to load it dynamically if it isn't loaded yet. Currently I have the following:
declare var MyLibrary:any;
export class MyLibraryService {
getInstance () : any {
if(MyLibrary === undefined) {
//load the library
} else {
return MyLibrary;
}
}
}
This throws the following error if MyLibrary doesn't exist yet.
ReferenceError: MyLibrary is not defined
Is there a way I can check if MyLibrary
is defined without throwing an exception?