我可以在TypeScript中获取当前行号

use*_*562 10 javascript typescript

我知道这听起来有点奇怪,但无论如何都要在TypeScript脚本中获取当前行号,以便将其发送到生成的JavaScript代码中?我想我正在寻找像C的预处理器这样的东西

__LINE__

变量.

编辑:我问的是TypeScript源文件中的当前行(通常与生成的JavaScript文件中的相应行号不同).

小智 3

我认为 souremaps 可以满足您的需要。Sourcemaps 是一种将 javascript 文件映射回其未修改状态的方法

如果您将打字稿编译器配置为包含源映射,则 Chrome 和其他开发工具可以引用您的打字稿文件。结果如下:

//index.ts

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

console.error('hey, here is an error');
Run Code Online (Sandbox Code Playgroud)

这会在 Chrome 开发工具的控制台中生成:

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

hey, here is an error index.ts:5

即使打字稿编译器会删除空行并重新格式化代码,行号也是正确的。

希望有帮助!

  • 那么在节点中呢? (7认同)