我剛剛開始使用TypeScript,並且遇到了我正在使用的庫的類型定義錯誤的情況。在我的情況下,圖書館是Massive,js和here are the type definitions。在TypeScript中,定義可返回Promise <T>或Promise <T[]>的函數的最佳方法是什麼?
問題是某些函數應返回Promise<T[]>
或Promise<T>
,但類型表示它總是Promise<T[]>
。
interface Table<T> {
// other functions omitted for simplicity
save(data: object): Promise<T[]>;
insert(data: object): Promise<T[]>;
update(dataOrCriteria: object, changesMap?: object): Promise<T[]>;
}
如何解決上述功能,這樣他們就返回要麼Promise<T[]>
或Promise<T>
?
你是怎麼用'Table'的?你在輸入嗎? –