微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Typescript "this" 隐式具有类型 "any",因为它没有类型注释 'this' implicitly has type

问题:

"this" 隐式具有类型 "any",因为它没有类型注释
'this' implicitly has type 'any' because it does not have a type annotation

解决方案:

将this放在函数形参上声明即可,使用的时候this不会干扰形参传入顺序

const Demo: React.FC = () => {

    const fn = () => {
        return function (this: any, ...args: any[]) {
            let that = this
            console.log(args); // 还是1,2,3
            return that
        }
    }
    fn()(1, 2, 3)

    return (
        <div className="wrapper">
        </div>
    )
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐