我有一個 ReactNative 應用程式,我正在嘗試使用 Jest 撰寫一個測驗。我的測驗需要使用來自本機組件(react-native-nfc-manager)的類,我需要的類宣告如下
export interface TagEvent {
ndefMessage: NdefRecord[];
maxSize?: number;
type?: string;
techTypes?: string[];
id?: number[];
}
如果我嘗試直接使用這個定義
const event = new TagEvent();
我收到這個錯誤
型別錯誤:_reactNativeNfcManager.default 不是建構式
我來自 Java 世界,所以我自己想 - 當然它不能實體化一個介面,它需要一個類,所以我為此介面撰寫了一個測驗實體:
class TestTagEvent implements TagEvent {
ndefMessage: NdefRecord[] = []
maxSize?: number;
type?: string;
techTypes?: string[];
id?: number[];
}
但問題似乎有所不同,因為它也不起作用:
型別錯誤:_TestTagEvent.TestTagEvent 不是建構式
我錯過了什么?
uj5u.com熱心網友回復:
您不能使用介面構造物件。介面只是一組屬性,可用于定義變數的必需/可用屬性。
要使用介面初始化變數,您需要這樣做:
const eventVar: TagEvent;
如果要將 TagEvent 用作可實體化的物件,則需要將其定義為類,而不是介面。像這樣,你定義TagEventProps的TagEvent介面在哪里:
class TagEvent {
constructor(props: TagEventProps) {
this.ndefMesssage = props.nDefMessage;
//etc...
}
}
uj5u.com熱心網友回復:
經過進一步的實驗,通過向類添加“匯出”解決了問題。誠然,原始錯誤描述絕對不符合根本原因。
匯出類 TestTagEvent 實作 TagEvent {
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403680.html
標籤:
下一篇:無法從異步存盤中檢索密鑰反應本機
