验证打字稿定义文件是否编程错误

发布于 2025-01-25 04:30:03 字数 1320 浏览 1 评论 0原文

我正在以编程方式生成打字稿定义文件。有时,将从用户错误生成错误的定义文件。这不能改变,无关紧要。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2025-02-01 04:30:03

昨天,我一直在摆弄这件事,以测试类型的挑战解决方案,这是我到目前为止发现的。

创建一个新程序:

import ts from "typescript";

const program = ts.createProgram({
    // ...
});

提供编译器选项:

const program = ts.createProgram({
    options: { ... },
});

提供输入点:

const program = ts.createProgram({
    // ...
    rootNames: ["./check-this-file.ts"],
});

然后调用emit并检查诊断:

const { diagnostics } = program.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:

import ts from "typescript";

const program = ts.createProgram({
    // ...
});

Provide the compiler options:

const program = ts.createProgram({
    options: { ... },
});

Provide the entry point:

const program = ts.createProgram({
    // ...
    rootNames: ["./check-this-file.ts"],
});

Then call emit and check the diagnostics:

const { diagnostics } = program.emit();

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 ????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文