Search code examples
typescriptbreezetypescript1.6

How to use create an instance of a class without importing it


I import most classes in my TypeScript app with the import statement:

import {Logger, getLogger} from "aurelia-logging";
import {HttpResponseMessage} from "aurelia-http-client";

class ErrorHandler {
    logger:Logger = getLogger("Error-Handler");
    handleError(message: any) : void {
        this.logger.error(message);
    }
    handleHttpError(response: HttpResponseMessage) {
        this.logger.error(response.content.error_description);
    }
}

However, I found some sample code that works that does this:

entityManager = new breeze.EntityManager(Settings.serviceName);

However, breeze is not imported anywhere.

I don't get how that works. There is a .d.ts file for breeze, but I don't see anywhere that it is imported anywhere in the project. Visual Studio recognizes "breeze" as a namespace, and EntityManager as a class. I believe it is getting that directly from the TDF.

class EntityManager {...}

Also the class is not exported. I thought that it had to be exported:

export class EntityManager{...}

It seems a lot simpler to not have to use the import/export statements, but I don't get how this is being achieved.


Solution

  • I don't get how that works. There is a .d.ts file for breeze, but I don't see anywhere that it is imported anywhere in the project

    It will work if the library pollutes (uses) the global namespace. The current breeze definition : https://github.com/borisyankov/DefinitelyTyped/blob/44cbde48eecd1918ed54b3be49a9752688b6c65a/breeze/breeze.d.ts works globally. Its a global module as there is no import/export at the root level (more).

    The runtime does too (by adding to window).