gpt4 book ai didi

typescript - 如何从emitDeclarationOnly获取TS编译器错误?

转载 作者:行者123 更新时间:2023-12-03 07:39:27 25 4
gpt4 key购买 nike

遵循TS文档:https://github.com/microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#getting-the-dts-from-a-javascript-file

const options: ts.CompilerOptions = {declaration: true, emitDeclarationOnly: true};
const host = ts.createCompilerHost(options);
host.readFile = () => 'import noexist from "noexist"; export const example = noexist();';
let output: string;
host.writeFile = (_, contents) => output = contents
const program = ts.createProgram(["example.ts"], options, host);
const emit = program.emit();
console.log(`Output:\n${output}`);
console.log(`Errors: ${emit.diagnostics.length}`);
它产生:
Output:
export declare const example: any;

Errors: 0
如何检索错误?

最佳答案

我发现它们是“发射前”错误:

const options: ts.CompilerOptions = {declaration: true, emitDeclarationOnly: true};
const host = ts.createCompilerHost(options);
host.readFile = () => 'import noexist from "noexist"; export const example = noexist();';
let output: string;
host.writeFile = (_, contents) => output = contents
const program = ts.createProgram(["example.ts"], options, host);
const emit = program.emit();
console.log(`Output:\n${output}`);
console.log(`Errors: ${ts.getPreEmitDiagnostics(program).length}`);
Output:
export declare const example: any;

Errors: 11

关于typescript - 如何从emitDeclarationOnly获取TS编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64828247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com