如果我在.ts文件中添加常规注释(/* */
或//
),则将其写入输出JS文件,除非我在汇编过程中指定removeComments
。
但是,我想知道是否有可能以相反的方式进行,除了"只有打字稿"的评论之外,请保留评论。这将类似于ASP.NET的Razor View Engine如何使用@* Comment *@
表示注释。
我知道我可以使用版权评论(/*!
)作为常规评论,而JavaScript注释仅作为typescript注释(这是用removeComments将其删除),但这似乎真的很尴尬,很难记住。
不使用其他构建工具/处理,打字稿编译器是否具有我忽略的方式,还是removeComments
是这里唯一适用的选择?
如果没有人找到一种"官方"的方法来执行此操作,则可以随时将评论放在您知道的代码部分中,这些评论将从发射的JavaScript中删除。这绝对是一个黑客,但根据您的用例,它可能对您有用:
interface TSOnlyComment {
// this comment will definitely not appear in the JS
}
这是一个名为TSOnlyComment
的虚拟空界面。您可以重复使用该界面进行多个注释,因为Typescript将其解释为接口合并:
interface TSOnlyComment {
// This only appears in TS, not JS
}
const javaScriptAwesomenessFactor = 1e9; // JS rules!
interface TSOnlyComment {
// Just kidding, TS is better than JS.
}
// let's print out how awesome JS is
console.log(javaScriptAwesomenessFactor);
interface TSOnlyComment {
// Ha ha, we are laughing at JS behind its back.
}
应该发出JS之类的东西(取决于您的--target
):
var javaScriptAwesomenessFactor = 1e9; // JS rules!
// let's print out how awesome JS is
console.log(javaScriptAwesomenessFactor);
那么,也许对您有用?祝你好运!