12

I know this sounds like a strange thing to ask, but is there anyway to get the current line number in a TypeScript script so that it will be emitted to the resulting JavaScript code? I guess I'm looking for something like C's preprocessor

__LINE__

variable.

Edit: I'm asking about the current line in the TypeScript source file (which will usually be different from the corresponding line number in the resulting JavaScript file).

user3700562
  • 693
  • 11
  • 23
  • Possible duplicate of [How can I determine the current line number in JavaScript?](http://stackoverflow.com/questions/2343343/how-can-i-determine-the-current-line-number-in-javascript) – Ingo Bürk Apr 24 '16 at 10:22
  • Typescript transpiles into Javascript, hence this question is equivalent to asking the same for Javascript. – Ingo Bürk Apr 24 '16 at 10:23
  • 10
    @IngoBürk nope, that's not necessarily the case.. Maybe he wants the current line in the typescript source code and not in the transpiled js code. What you can do, I guess, is use the solution of getting the js line number and then use the source map to get the line in the typescript source code. – Nitzan Tomer Apr 24 '16 at 10:24
  • @NitzanTomer True. Reading the question I can't tell which one it is, which means it's unclear what is being asked instead now. :-) I'll retract my vote if OP clarifies that this is what they want. – Ingo Bürk Apr 24 '16 at 10:36
  • Sorry, I edited the question. I am indeed asking about the current line number in the TypeScript source file. – user3700562 Apr 24 '16 at 10:56
  • Well, as I wrote, you'll need to use the source map (the `.js.map` file the compiler outputs). It will help if you'll clarify on the scenario. – Nitzan Tomer Apr 24 '16 at 10:58
  • 1
    I know it is a bit late - but I had this same question and came up with a solution - I hope it helps you some - [Posted it over in my question](http://stackoverflow.com/questions/38194457/how-to-get-actual-line-within-source-for-custom-logging-in-typescript/38197778#38197778). – ehiller Jul 05 '16 at 07:39
  • Same problem here. Any solution found in the end? Thanks. – luthien Apr 03 '17 at 09:26
  • Yes, I ended up parsing the .js.map files which worked fine. – user3700562 Apr 05 '17 at 09:48
  • Compile-time info like this would be a fantastic addition to TypeScript to put it further ahead of Javascript IMHO. It's a frustrating omission from Javascript. – spechter Apr 03 '18 at 04:02

1 Answers1

2

I think souremaps will do what you need. Sourcemaps are a way to map a javascript file back to it's unmodified state.

If you configure your typescript compiler to include sourcemaps then Chrome and other dev tools can reference your typescript files. The result would look like this:

//index.ts

console.log('hey, here is a log!');

console.error('hey, here is an error');

Which would produce this in the console of Chrome's dev tools:

hey, here is a log! index.ts:3

hey, here is an error index.ts:5

The line numbers would be correct even though the typescript complier would strip out the empty lines and reformat the code.

Hope that helps!