簡體   English   中英

如何在Typescript中為模塊鍵入變量?

[英]How to type a variable to a module in Typescript?

給定文件

路徑\\ Example.ts:

export module Example {
    ... Stuff ...
}

和Test.ts:

import { Example }    from "Path/Example";

let exampleMock = getExampleMock(); // getExampleMock returns an any that matches the type structure of Example

let e = exampleMock as Example; // Errors: Cannot find name 'Example'
let local = Example; // local is typed as Example;
local = exampleMock ; // since exampleMock is an any, compiler allows this call.

... use local with full typing of Example module 

有沒有辦法向模塊輸入變量,而不先將其設置為真實模塊?

嘗試

let exampleMock = getExampleMock() as typeof Example;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM