首页 > 其他分享 >how to fix TypeScript error 'this' implicitly has type 'any' All In One

how to fix TypeScript error 'this' implicitly has type 'any' All In One

时间:2023-02-23 02:22:05浏览次数:39  
标签:TypeScript thisObj log fix console type any

how to fix TypeScript error 'this' implicitly has type 'any' All In One

'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)

'this' 隐式具有类型 'any' 因为它没有类型注释。

errors ❌

const nums = [1, 2, 3];
nums.myForEach(function(a, b, c, thisObj) {
  console.log(`a, b, c =`, a, b, c);
  console.log(`thisObj =`, thisObj);
  console.log(`this = `, this);
});

image

solution

fix: TypeScript & ES5 function this error, 'this' implicitly has type 'any'

✅ just add this: any as the ES5 function's first argument.

只需要把 this:any 添加到函数的第一个参数位置即可,这不会影响后面正常参数的传入顺序!

const nums = [1, 2, 3];
nums.myForEach(function(this: any, a, b, c, thisObj) {
  // A 'this' parameter must be the first parameter.ts(2680) ✅
  console.log(`a, b, c =`, a, b, c);
  console.log(`thisObj =`, thisObj);
  console.log(`this = `, this);
});

image

demos

test ✅

image

https://www.typescriptlang.org/play?#code/PQKhAIAEBcE8AcCm4DeBZWB5ARgKwL7gjABQAxgPYB2AztOBeALyonjvgBmFFAXFwFcqZaAEtqACgCUrDnPATOQkeKoToAC1E1+AQyqwZKNvNOVaFADaIAdJYoBzdVppSA3OGDBwgUHITp-BtsUSoAE2dtKSlpN39wfBJ8WJIQ6EQAJ05dMmQMHFxZDm4KCJ1UcF1+KgEAW2wM+Kl+ADcKUVDY-CA

(

标签:TypeScript,thisObj,log,fix,console,type,any
From: https://www.cnblogs.com/xgqfrms/p/17146574.html

相关文章

  • typescript中的type和interface的区别
    //1,写法不一样typeMyType={name:stringage:number}interfaceMyType{name:string;age:number}//2,拓展方式不一样typeMyType={name:......
  • AnyShare客户端问题排查
    手动安装客户端服务1、cmd窗口创建anyshareservice服务先打开服务,检查服务状态,并开启尝试手动开启以管理员身份打开命令提示符,删除已有Anyshare服务scquery查询所有......
  • 【TypeScript 4.5】007-第 7 章 类型操纵
    【TypeScript4.5】007-第7章类型操纵文章目录​​【TypeScript4.5】007-第7章类型操纵​​​​一、从类型中创建类型​​​​1、概述​​​​2、方法​​​​二、泛......
  • 【TypeScript 4.5】006-第 6 章 对象类型
    【TypeScript4.5】006-第6章对象类型文章目录​​【TypeScript4.5】006-第6章对象类型​​​​一、认识对象类型​​​​1、概述​​​​说明​​​​对象类型​​......
  • 【TypeScript 4.5】004-第 4 章 类型缩小
    【TypeScript4.5】004-第4章类型缩小文章目录​​【TypeScript4.5】004-第4章类型缩小​​​​一、typeof类型守卫​​​​1、什么是类型缩小​​​​含义​​​​......
  • 【TypeScript 4.5】003-第 3 章 常用类型
    【TypeScript4.5】003-第3章常用类型文章目录​​【TypeScript4.5】003-第3章常用类型​​​​一、基元类型string、number和boolean​​​​1、基本含义​​​......
  • 【TypeScript 4.5】001-第 1 章 TypeScript 介绍
    【TypeScript4.5】001-第1章TypeScript介绍文章目录​​【TypeScript4.5】001-第1章TypeScript介绍​​​​一、什么是TypeScript​​​​二、JS、ES以及TS的关......
  • 【TypeScript 4.5】002-第 2 章 TypeScript 入门
    【TypeScript4.5】002-第2章TypeScript入门文章目录​​【TypeScript4.5】002-第2章TypeScript入门​​​​一、发现问题​​​​1、字符串​​​​2、函数​​​......
  • 【TypeScript 4.5】005-第 5 章 函数
    【TypeScript4.5】005-第5章函数文章目录​​【TypeScript4.5】005-第5章函数​​​​一、函数类型表达式​​​​1、概述​​​​函数​​​​函数类型表达式​​......
  • 【TypeScript 编程】001-002 第 1 章 导言 与 第 2 章 TypeScript 概述
    【TypeScript编程】001-002第1章导言与第2章TypeScript概述文章目录​​【TypeScript编程】001-002第1章导言与第2章TypeScript概述​​​​第1章......