Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the current line number in TypeScript

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).

like image 377
user3700562 Avatar asked Apr 24 '16 10:04

user3700562


People also ask

How to get line number in js?

To determine the current line number in JavaScript, we can get the lineNumber property from an Error object. const thisLine = new Error(). lineNumber; to create a new Error object and get the lineNumber property from it to get the line number of the current line.


1 Answers

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!

like image 114
justinschuldt Avatar answered Oct 13 '22 05:10

justinschuldt