美文网首页
ts踩坑记录

ts踩坑记录

作者: 如果俞天阳会飞 | 来源:发表于2022-04-26 15:03 被阅读0次

    错误信息 TS7053

    const person = {
        name: '张三',
        age: 10
    };
    
    function getValue(arg: string) {
        return person[arg];
    }
    
    
     TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ isMobile: string; isUnion: string; isTelcom: string; no: s
    tring; }'.
    
    

    解决方法1:

    在tsconfig.json中配置suppressImplicitAnyIndexErrors: true

    {
        "compilerOptions": {
            "suppressImplicitAnyIndexErrors": true,
            ...
        },
        ...
    }
    

    解决方法2: 给person定义接口

    const person = {
        name: '张三',
        age: 10
    };
    
    function getValue(arg: string) {
        interface IPerson {
            [key: string]: any
        }
        return (<IPerson>person)[arg];
    }
    

    来源: https://blog.csdn.net/lihefei_coder/article/details/103694047

    相关文章

      网友评论

          本文标题:ts踩坑记录

          本文链接:https://www.haomeiwen.com/subject/zaacyrtx.html