![](/img/trans.png)
[英]How to prevent TypeScript from including .d.ts import in the output?
[英]Stop TS including import of definition file in output
我有一個第3方庫,該庫通過身份驗證密鑰動態加載。
這是一個相當復雜的API,因此我想在TypeScript代碼中使用類型定義。
在我的.tsconfig
我正在使用"target": "esnext"
和"module": "esnext"
。
使用類型定義typings/third.party.d.ts
我在TS代碼中輸入了以下內容:
import * as third_party from './typings/third.party';
問題是最終出現在JS輸出中-我不希望這樣,當源文件是.d.ts
時也不應該.d.ts
。
我試過包括擴展名:
import * as third_party from './typings/third.party.d.ts';
但這會使TS錯誤代碼TS2691: “導入路徑不能以'.d.ts'擴展名結尾。請考慮導入'./typings/third.party'。”
我嘗試添加:
/// <reference path="typings/third.party.d.ts" />
而是在文件的頂部,但是根本找不到。
我知道我可以使用各種.d.ts
器解決這個問題,但是我不希望依賴-我希望TS編譯器從.d.ts
文件強制執行定義,但根本不嘗試將其包含在export .js
文件中。
我怎么做?
我找到了一種解決方法,但不確定是否是一個錯誤,因此寧願有完整的答案。
typings/third.party.d.ts
文件包含一個export = ...
語句,如果沒有,該語句將無法與import
語句一起使用,但是這似乎阻止了它/// <reference path=...
聲明。
看起來/// <reference path=...
不適用於任何包含顯式默認模塊定義的內容(盡管它可以包含export module
和export class
)。
即使文件顯式為.d.ts
,看起來import
也被視為輸出依賴.d.ts
,我認為這可能是TS中的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.