【问题标题】:How can I grep/pipe/redirect the output of tsc (TypeScript compiler)?如何 grep/管道/重定向 tsc(TypeScript 编译器)的输出?
【发布时间】:2020-09-24 12:08:58
【问题描述】:

我的项目包含一个非常简单的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 的完整输出?

【问题讨论】:

标签: typescript shell pipe io-redirection tsc


【解决方案1】:

使用--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,
    },
}

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 2012-10-05
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2011-07-22
    • 2021-09-10
    • 2012-09-25
    • 2017-09-15
    相关资源
    最近更新 更多