首页 > 其他 > 详细

typescript 编译报错 不能用于索引类型

时间:2020-07-02 16:11:58      阅读:1224      评论:0      收藏:0      [点我收藏+]

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);
  });
};

技术分享图片

typescript 编译报错 不能用于索引类型

原文:https://www.cnblogs.com/sjie/p/13225107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!