9

From other languages I know __FILE__ or similar expressions (for instance: in PHP a constant, in C a preprocessor macro) that are replaced with the current source file path. Is there anything like this in TypeScript?

2 Answers 2

3

If you are creating a node application

__dirname

Will give you the current module directory path

https://nodejs.org/docs/latest/api/globals.html#globals_dirname

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, it’s not a Node app. Sorry for not mentioning it in the question.
2

There is currently feature request for that, see https://github.com/Microsoft/TypeScript/issues/4892

From what I am seeing it is not implemented yet.

One work-around can be, adding your custom comments( for example /** @compile-if */) and then using Gulp/Grunt task to transform code after actual Typescript compilation.

1 Comment

Too bad. I asked because I hoped I could avoid having to build a home-made solution for this. Doesn’t look like I can.

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.