typescript 編譯報錯 不能用於索引類型


Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'typeof

備注: 元素隱式地具有“ any”類型,因為類型“ string”的表達式不能用於索引類型“ typeof import(“xxx”)

解決辦法

mock.ts

export function text(): boolean {
  return false;
}

export function setAasd(): string {
  return "asdgjhkj";
}

request.ts

interface MockIndex {
  text: "text";
}
import * as Mock from "@/api/Mock.ts";
const request = (MockIndex: keyof MockIndex) => {
  // 模擬異步請求數據
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Mock[MockIndex]);
    }, 100);
  });
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM