2 回答

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊
當(dāng).d.ts文件使用exportorimport時,它們被視為 amodule而不是ambient typings。
但是,您仍然可以使用declare global. 這允許您擴(kuò)充全局類型,甚至添加新的全局類型:
.d.ts
import { SomeValue } from "./SomeModule";
declare global {
interface Window {
$: () => SomeValue;
}
interface SomeGlobalInterface {
x: number;
}
}
.ts
// () => SomeValue
window.$
let value!: SomeGlobalInterface;
value.x;

TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊
您需要將“export”關(guān)鍵字添加到變量中。
export declare var $: () => SomeValue;
如果您希望此變量僅在同一文件中可用,則無需添加“export”關(guān)鍵字。但是,如果您希望它在其他文件中使用,則需要添加“export”關(guān)鍵字。
常量、函數(shù)和類也是如此。向其中添加“導(dǎo)出”將使其可供其他文件導(dǎo)入。
添加回答
舉報