如何grep / pipe /重定向tscTypeScript编译器的输出?

问题描述

我的项目由一个非常简单的index.ts组成:

const answer: 42 = 43;

如果我对其进行编译,则会出现预期的类型错误(带有颜色;此处未显示):

$ tsc
index.ts:1:7 - error TS2322: Type '43' is not assignable to type '42'.

1 const answer: 42 = 43;
        ~~~~~~


Found 1 error.

比方说,我想grep输入类型错误

$ tsc | grep "const answer"

我没有结果。难怪:

$ tsc | wc -l
1
$ tsc | cat
index.ts(1,7): error TS2322: Type '43' is not assignable to type '42'.

重定向没有帮助:

$ tsc 2>&1 | wc -l
1
$ tsc 2>&1 | cat
index.ts(1,7): error TS2322: Type '43' is not assignable to type '42'.

如何在终端中访问tsc的完整输出

解决方法

使用--pretty标志:

$ tsc --pretty | grep "const answer"
1 const answer: 42 = 43;

(请注意,"--pretty output is not meant to be parsed",请谨慎使用。)

它是这样的:

$ tsc --pretty false
index.ts(1,7): error TS2322: Type '43' is not assignable to type '42'.
$ tsc --pretty true
index.ts:1:7 - error TS2322: Type '43' is not assignable to type '42'.

1 const answer: 42 = 43;
        ~~~~~~


Found 1 error.

默认启用,"unless piping to another program or redirecting output to a file"

您也可以在tsconfig.json中指定它:

{
    "compilerOptions": {
        "pretty": true,},}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...