77

I'm trying to do this, but it's not working like I'd expect.

(I'm using the AMD option)

//logger.ts
export class Logger {

    static log(message: string) {
        //do stuff
    }
}

//main.ts
import logger = module('services/logger');
logger.log("test"); //The property 'log' does not exist on value of type '"logger"'
logger.Logger.log(); //works

How do you do logger.log()?

1
  • that should work fine, I have similar code working ( perhaps the TS versioning solved it ) Commented Jan 11, 2016 at 15:35

2 Answers 2

176

You can import classes directly, which allows you to have the usage you want.

// usage
import { Logger } from 'path/logger.ts'
Logger.Log();

And the definition stays the same.

// path/logger.ts
export class Logger {

    static Log() {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

13

This answer was correct at time of posting. It is now deprecated. See Dimitris' answer for a better current solution.

Using a class, you can't. You're always going to have to call {module}.{class}.{function}

But you can drop the class altogether and just call {module}.{function}:

// services/logger.ts
export function log(message:string){
 // do stuff
}

//main.ts
import logger = module('services/logger');
logger.log("test"); // Should work

10 Comments

The changes to how internal modules contribute to the type system aren't relevant here.
I wasn't clear if it was only internal modules that were changing. Thanks - I'll update.
I use this all the time for modules that are in fact singletons.
The property 'log' does not exist on value of type "logger"
I would prefer to access through a class wrapper, but I don't want to have to do module.class.function, I wanted the import to alias directly to the class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.