[英]Providing an index.d.ts file for canonical-json?
我正在嘗試為canonical-json
創建一個index.d.ts
文件。 這就是我所擁有的:
declare module 'canonical-json' {
export function stringify(s: any): string;
}
還嘗試了:
declare namespace JSON {
export function stringify(s:any):string;
}
export = JSON;
和
export as namespace JSON;
export const stringify: (o:any) => string;
但是我得到:
canonical_json_1.stringify不是一個函數
對於所有三個嘗試。
這是stackblitz: https ://stackblitz.com/edit/typescript-cryptojs ? file = src%2F%40types%2Fcanonical-json%2Findex.d.ts
由於這是一個通用的js模塊,因此整個導出都是一個對象,因此您可以使用特定的export =
語法:
// src\@types\canonical-json\index.d.ts
declare module 'canonical-json' {
function stringify(s: any): string;
export = stringify;
}
// index.ts
import stringify = require('canonical-json');
您還可以啟用"esModuleInterop": true
以將導出作為默認導入進行訪問:
// src\@types\canonical-json\index.d.ts
declare module 'canonical-json' {
function stringify(s: any): string;
export = stringify;
}
// index.ts
import stringify from 'canonical-json';
最后一個選擇是使定義僅具有默認導出,您仍然需要"esModuleInterop": true
因為模塊實際上沒有默認導出:
// src\@types\canonical-json\index.d.ts
declare module 'canonical-json' {
export default function stringify(s: any): string;
}
// index.ts
import stringify from 'canonical-json';
注意:我在節點中測試了所有這些配置,但我希望在其他環境中也能使用相同的配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.