Carmineprince's Blog
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档

Ziqi Wang

胡思乱想程序员
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档
  • ts学习记录1--类型
  • Cannot find module
  • 使用动态变量ts报错的解决
    • TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.   No index signature with a parameter of type 'string' was found on type '{}'.
    • 解决
  • TypeScript相关
carmineprince
2022-02-25

使用动态变量ts报错的解决

# 使用动态变量ts报错的解决

# TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.   No index signature with a parameter of type 'string' was found on type '{}'.

export const cleanObject = (object: object) => {
  const result = { ...object };
  for (const [key, val] of Object.entries(result)) {
    if (!val) {
      delete result[key]; // result[key]报错,具有隐性any
    }
  }
  return result;
};
1
2
3
4
5
6
7
8
9

# 解决

export const cleanObject = (object: object) => {
  type Result = {
    [key: string]: any;
  };
  const result: Result = { ...object };
  for (const [key, val] of Object.entries(result)) {
    if (!val) {
      delete result[key];
    }
  }
  return result;
};
1
2
3
4
5
6
7
8
9
10
11
12
#typescript
上次更新: 3/2/2022, 6:42:33 PM
Cannot find module

← Cannot find module

最近更新
01
pc端rem配置
03-02
02
React Hook详解
02-18
03
Module "xxx" does not exist in container
02-17
更多文章>
Theme by Vdoing | Copyright © 2021-2022 Carmineprince | 鲁ICP备2021046263号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式