首页 > 其他分享 >[Typescript] This type

[Typescript] This type

时间:2023-12-30 22:11:38浏览次数:384  
标签:event Typescript myClickHandler Event new type click

Sometimes we have a free-standing function that has a strong opinion around what this will end up being, at the time it is invoked.

For example, if we had a DOM event listener for a button:

<button onClick="myClickHandler">Click Me!</button>

 In the following handler, thethis type is any.

 

Oh no! this is an any type. We sure don’t want to depend on disabled being a defined property without some degree of type safety. If we enable the compilerOptions.noImplicitThisflag in tsconfig.json, you’ll see a type checking error here

 

To address the problem, we need to give this function a this type

function myClickHandler(
  this: HTMLButtonElement,
  event: Event
) {
  this.disabled = true
}

myClickHandler(new Event('click')) // The 'this' context of type 'void' is not assignable to method's 'this' of type 'HTMLButtonElement'.

 

To resolve this issue, we need to manually bind:

function myClickHandler(
  this: HTMLButtonElement,
  event: Event
) {
  this.disabled = true
}

myClickHandler(new Event("click")) // not working

const myButton = document.getElementsByTagName("button")[0]           
const boundHandler: (event: Event) => void = myClickHandler.bind(myButton)
boundHandler(new Event("click")) // bound version: ok
myClickHandler.call(myButton, new Event("click")) // also ok

   

标签:event,Typescript,myClickHandler,Event,new,type,click
From: https://www.cnblogs.com/Answer1215/p/17936920

相关文章

  • 解决 typescript node tsx 的兼容问题
    问题在项目中使用typescript+tsx+node存在各种兼容问题,包括:[esbuildError]:Top-levelawaitiscurrentlynotsupportedwiththe"cjs"outputformatCannotfindmodule'X'.Didyoumeantosetthe'moduleResolution'optionto'......
  • HTTP协议安全头部X-Content-Type-Options引入的问题
    本文于2016年4月完成,发布在个人博客网站上。考虑个人博客因某种原因无法修复,于是在博客园安家,之前发布的文章逐步搬迁过来。前段时间测试MM反馈了一个问题,在富文本编辑器里上传的图片无法正常呈现。因为Jackie在本机的环境上没有观察类似的现象,而恰好那天测试环境的某个重要配......
  • TypeORM 的同步选项
    配置TypeORM连接到数据库时,有一个synchronize选项,意思是“同步”,也就是同步TypeORM实体和数据表,再详细讲就是根据实体自动创建和修改数据表结构,使其与TypeORM定义的实体类相匹配。以Nest中的使用为例:import{Module}from'@nestjs/common';import{TypeOrmModule}......
  • Spring BeanFactoryAware 解决 prototype 作用域失效问题
    跟着孙哥学Spring,b站:https://www.bilibili.com/video/BV185411477k/?spm_id_from=333.337.search-card.all.click在Spring中,如果一个singletonbean依赖了一个prototypebean,那么这个prototypebean在初始化时只会被创建一次,这就是所谓的"prototypescope失效"的问题......
  • python中的泛型使用TypeVar
    1.引入为什么需要TypeVarPEP484的作者希望借助typing模块引入类型提示,不改动语言的其它部分。通过精巧的元编程技术,让类支持[]运算不成问题。但是方括号内的T变量必须在某处定义,否则要大范围改动python解释器才能让泛型支持特殊的[]表示法。鉴于此,我们增加了typing.TypeVar构造......
  • 项目启动报错 No Spring Session store is configured: set the ‘spring.session.sto
    NoSpringSessionstoreisconfigured:setthe‘spring.session.store-type‘property项目启动为啥会报这个错误?这个错误信息表明你的SpringBoot应用程序没有配置SpringSession的存储类型。SpringSession是Spring框架提供的一个用于管理HTTP会话的组件,它可以与不同的存储......
  • 项目中使用spring.session.store-type=redis和@EnableRedisHttpSession 的区别
    spring项目的session存在哪里SpringSession提供了多种存储策略,可以选择将session存储在内存、数据库或Redis缓存中。内存存储:这是默认的存储方式,适用于单个应用程序的情况。SpringSession会在应用程序启动时创建一个ConcurrentHashMap对象,用于存储session数据。JDBC存......
  • [C++ 从入门到精通] 16.RTTI、dynamic_cast、typeid、虚函数表
    文章预览:一.RTTI是什么二.dynamic_cast类型(指针/引用)转换2.1C风格的强制类型转换2.2指针转换(常见用法)2.3引用转换三.typeid运算符四.type_info类五.RTTI与虚函数表一.RTTI是什么RTTI(Run-TimeTypeIdentification):通过运行时类型信息,程序能够使用基类的指针或引用来检查......
  • typeScript中map和filter的用法
    首先,这两个方法map()和filter()都是对调用他们的数组进行遍历。那么在项目中,什么情况下使用map(),又在什么情况下使用filter()呢?1、map()的使用方法:arr.map((item,index,array)=>{……})2、filter()的使用方法:arr.filter((item,index,array)=>{……})都是将函数写在方法的......
  • 常见的文件后缀对应的ContentType
    #LicensedtotheApacheSoftwareFoundation(ASF)underoneormore#contributorlicenseagreements.SeetheNOTICEfiledistributedwith#thisworkforadditionalinformationregardingcopyrightownership.#TheASFlicensesthisfiletoYouunderth......