我正在嘗試修復youtube-dl-exec的 Typescript 宣告,它有一個默認匯出,它是一個具有屬性的函式。簡而言之,默認匯出回傳一個承諾,或者您可以呼叫exec()回傳子行程的方法,這樣您就可以監控進度而無需等待承諾解決。
現有宣告適用于默認函式,但不適用于任何屬性:
import ytdl from 'youtube-dl-exec';
const p = ytdl('https://example.com'); // p is Promise<YtResponse>
const r = ytdl.exec('https://example.com'); // r should be ExecaChildProcess
// ^ property exec does not exist … ts(2339)
到目前為止,我想出的是這個宣告:
declare module 'youtube-dl-exec' {
type ExecaChildProcess = import('execa').ExecaChildProcess;
const youtubeDl = (url: string, flags?: YtFlags, options?: Options<string>) => Promise<YtResponse>;
youtubeDl.exec = (url: string, flags?: YtFlags, options?: Options<string>) => ExecaChildProcess;
export default youtubeDl;
}
exec現在Typescript 可以看到的存在和簽名,但回傳型別為any. 我不認為型別匯入是一個問題,事實上即使我將回傳型別更改為原始型別,stringTypescript 編譯器仍然會將回傳型別exec視為 `any.
由于函式屬性的賦值幾乎可以作業,我覺得我在正確的軌道上,但不知道如何指定這個函式屬性的回傳型別。
uj5u.com熱心網友回復:
這通過使用命名空間實作
type ExecaChildProcess = import('execa').ExecaChildProcess;
declare const youtubeDl: (url: string, flags?: YtFlags, options?: Options<string>) => Promise<YtResponse>;
declare namespace youtubeDl {
export const exec: (url: string, flags?: YtFlags, options?: Options<string>) => ExecaChildProcess;
}
export default youtubeDl;
}
或將函式型別與物件型別合并
declare module 'youtube-dl-exec' {
type ExecaChildProcess = import('execa').ExecaChildProcess;
declare const youtubeDl: (
(url: string, flags?: YtFlags, options?: Options<string>) => Promise<YtResponse>
) & {
exec: (url: string, flags?: YtFlags, options?: Options<string>) => ExecaChildProcess;
}
export default youtubeDl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521752.html
上一篇:獲取不包含特定鍵的物件
下一篇:函式引數的可選鏈接
