验证打字稿定义文件是否编程错误
我正在以编程方式生成打字稿定义文件。有时,将从用户错误生成错误的定义文件。这不能改变,无关紧要。
export type Test = {
prop: number
}
export interface Example {
test: string; // Error since not of type Test
[k: string]: Test;
}
我正在尝试通过编程方式检查此定义文件,以查看我是否遇到错误。例如,如果我创建一个用于测试目的的文件,则单个导入;
import * as TestImport from './example';
然后使用命令行中的打字稿编译器,我将按预期遇到错误;
$ npx tsc --noEmit test.ts
$ error TS2411: Property 'test' of type 'string' is not assignable to 'string' index type 'Test'.
5 test: string;
~~~~~~~~~~~~~
这是一个不错的步骤,也许作为倒退,我将简单地研究npx
以编程方式使用。
但是,目前,我试图直接使用tsc
,但是我在编译时没有遇到错误。
import { transpile } from "typescript";
import { readFile } from "fs/promises"
const data: string = (await readFile(`./example.ts`)).toString();
transpile(data, { strict: true });
我也尝试使用transpilemodule
,但没有。我还尝试出于绝望而尝试动态导入,但是Runtime Imports显然不会影响打字稿的定义。
我很认为,为什么命令行tsc
似乎与定义发现了此导入问题,但transpile
却没有。 transpile
甚至是正确使用的方法吗?我是否缺少汇编选项?我是否需要写一个基本自定义编译器?
I am generating typescript definition files programmatically. Sometimes an incorrect definition file will be generated from user error. This cannot be changed and is irrelevant.
export type Test = {
prop: number
}
export interface Example {
test: string; // Error since not of type Test
[k: string]: Test;
}
I am trying to programmatically check this definition file to see if I get errors. For example, if I create a file for testing purposes with a single import;
import * as TestImport from './example';
and then use the typescript compiler from command line, I will get an error as expected;
$ npx tsc --noEmit test.ts
$ error TS2411: Property 'test' of type 'string' is not assignable to 'string' index type 'Test'.
5 test: string;
~~~~~~~~~~~~~
This is a good step, and perhaps as a fall back I will look into simply using npx
programmatically.
However, for now I am trying to use tsc
directly, but I am getting no error on compilation;
import { transpile } from "typescript";
import { readFile } from "fs/promises"
const data: string = (await readFile(`./example.ts`)).toString();
transpile(data, { strict: true });
I tried using transpileModule
too but nothing. I also tried dynamic imports out of desperation, but runtime imports obviously don't influence typescript defintions.
I'm stumped as to why tsc
from the command line seems to find this import issue with definitions but transpile
does not. Is transpile
even the correct method to use? Am I missing compilation options? Do I need to write a basic custom compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
昨天,我一直在摆弄这件事,以测试类型的挑战解决方案,这是我到目前为止发现的。
创建一个新程序:
提供编译器选项:
提供输入点:
然后调用
emit
并检查诊断:它将是一系列诊断对象,您可以登录并查看其中包含的内容(诊断消息,位置,代码等)。
如果您问我,一个非常基本的自定义编译器
I've been fiddling around with this yesterday to test type challenge solutions and here's what I found so far.
Create a new Program:
Provide the compiler options:
Provide the entry point:
Then call
emit
and check the diagnostics:It will be an array of Diagnostic objects which you can log and see what's included in there (diagnostic message, location, code, etc).
A pretty basic custom compiler if you ask me ????