如何编译 .d.ts(打字稿) [英] How to compile .d.ts (typescript)
问题描述
我正在尝试创建一个 TypeScript 模块,但我有以下问题:- 当我在另一个项目中安装这个模块并使用它时,我收到SyntaxError: Unexpected token export"错误.这个错误是由 index.d.ts 文件引起的,它有这样的行```
I am trying to create a TypeScript module but I have the following issues: - When I install this module in another project and use it i receive "SyntaxError: Unexpected token export" error. This error is caused by index.d.ts file that have line line like this ```
export * from './logger' // (ES6)
出现问题的原因可能是节点在寻找 ES5 代码.
The issue appear because probably the node looking for ES5 code.
我试图通过思考(并在互联网上搜索)一种告诉 TypeScript 编译器编译 .d.ts 文件的方法来解决这个问题,我发现了一个通过生成类型( "declaration": true )编译的选项.之后我必须手动复制(cp -r src/types lib/types)来解决lib文件夹中缺少类型的问题.
I tried to solve this issue by thinking (and searching on internet) on a way to tell to TypeScript compiler to compile .d.ts file and I found an options that compile by generate types ( "declaration": true ). After that i have to copy manually ( cp -r src/types lib/types) to solve the issue with missing types in lib folder.
我现在唯一的问题是,我无法在项目中导入类型,因为我安装了模块,因为文件是用 es6 写入的
The only issue that i have now is , i cannot import types in the project in that i installed the module because the file is write with es6
我认为我的方法是错误的.(是我写的第一个typescrypt模块)
I think my approach is wrong. (Is the first typescrypt module written by my)
谢谢.
节点:我的 tsconfig.json 文件是 https://pastebin.com/zzCs88ZM
Nodes: My tsconfig.json file is https://pastebin.com/zzCs88ZM
推荐答案
Afaik .d.ts 文件不是为了编译(d 代表声明).它们只是类型定义文件,旨在为您的 IDE/编辑器提供正确的语法、代码突出显示、自动完成等.
Afaik .d.ts files are not meant to be compiled (the d stands for declaration). They are simply type definition files and are meant for your IDE / Editor to provide you with proper syntax, code highlight, autocomplete, etc.
如果您需要将其编译为常规 .ts 文件.在你的情况下,如果你想让记录器可用,它肯定应该是一个常规的 .ts 文件.
If you need it compiled make it to a regular .ts file. And in your case if you want to have the logger available it should definitively a regular .ts files.
d.ts 文件不应包含逻辑.
d.ts files should not contain logic.
也许您可以在此处找到有关定义文件的一些帮助:https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
Maybe you find some help regarding your definition file here: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
这篇关于如何编译 .d.ts(打字稿)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!