gpt4 book ai didi

typescript - 如何从 TypeScript 中完全隐藏不相关的类型声明目录?

转载 作者:行者123 更新时间:2023-12-05 02:28:51 26 4
gpt4 key购买 nike

我正在努力改进我的 tsc通过减少 TypeScript 引入的源文件数量来缩短构建时间和编辑器响应速度。我看到很多 .d.ts node_modules 中的文件它们是通过传递依赖项引入的,我想要一种在编译期间将它们排除在考虑之外的方法。

为了使这个更具体:我正在使用 PgTyped为我的 SQL 查询生成 TypeScript 类型声明。要使用它,我需要安装两个依赖项:

npm install @pgtyped/query @pgtyped/cli

@pgtyped/query包取决于 antlr4ts ,这ships with许多捆绑.d.ts自己的文件。这些被拉进了我的node_modules目录:

$ ls node_modules/antlr4ts/**/*.d.ts | wc -l
179

其中一些类型确实通过node_modules/@pgtyped/query/lib/index.d.ts被引入.我可以通过使用 tsc --listFiles 来查看:

$ tsc --noEmit --listFiles | grep antlr4ts
/myrepo/ts/node_modules/antlr4ts/atn/ATNStateType.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/Stubs.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/IntegerList.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/Interval.d.ts
...

据我所知(著名的遗言!)这些都与 PgTyped 的公共(public) API 无关。如果我只是将它们替换为 any ,我的应用程序不会有明显的差异,并且文件会比 tsc 少 ~100 个需要加载。

我尝试了 TypeScript Wiki 的 Performance Page 中建议的一些技巧,即包括**/node_modules在我的 exclude :

{
"compilerOptions": {
"lib": [ "dom", "DOM.Iterable", "es2019" ],
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true,
// ... many other settings ...
},
"exclude": [
"./cypress",
"**/node_modules",
"**/.*/"
]
}

我设置了skipLibCheck并尝试创建一个声明文件来删除 antlr4ts :

// declarations/antlr4ts.d.ts
declare module 'antlr4ts';

此文件出现在 tsc --listFiles 的输出中,但仅在 antlr4ts 的所有 110 个之后声明文件。

是否有可能阻止 TypeScript 看到这些 .d.ts文件?理想情况下,我想让它提供所有这些 antlr4ts模块 any类型。

最佳答案

为了提高您的编辑器响应能力,您可以尝试将带有excludeDirectorieswatchOptions 添加到您的tsconfig 文件(doc ).这样,您可以告诉您的编辑器不应该在 node_modules 中搜索。从 Typescript webpage 开始:

TypeScript 3.8 introduces a new watchOptions field which allows users to tell the compiler/language service which watching strategies should be used to keep track of files and directories.

关于构建时间,typescript 不会编译您的node_modules 中的文件,它只会查找类型声明文件(d.ts)。这应该很快(几秒钟),但目前似乎不是忽略这些文件的解决方案。要减少构建时间,您可以做的一件事是在编译时使用 Typescript's incremental mode。这会将编译器信息保存到您的项目中并加速以下编译。

关于typescript - 如何从 TypeScript 中完全隐藏不相关的类型声明目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72495251/

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