這是一個函式:
function factoryTableView(category: number) {
const views = {
1: new ImportExportTable()
};
return views[category];
}
我收到此錯誤訊息:
Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{ 1: ImportExportTable; }'.
No index signature with a parameter of type 'number' was found on type '{ 1: ImportExportTable; }'.
如何解決?
uj5u.com熱心網友回復:
除非您另外指定,否則物件會隱含地為其屬性提供字串。
views如果要使用數字作為屬性名稱,請顯式定義型別。
function factoryTableView(category: number) {
const views: Record<number, ImportExportTable> = {
1: new ImportExportTable()
};
return views[category];
}
(它們在 JS 中仍然是字串,但 TypeScript 將要求您使用數字來訪問它們,并且它們只會在運行時轉換為字串)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/508370.html
標籤:打字稿
