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