TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'


ts使用枚举类型引用报错

解决方案一:在对象接口中使用 keyof typeof 枚举变量
    enum color {
        b = 'black',
        w = 'white',
        g = 'green'
    }
    interface Style{
        bg:keyof typeof color,
        [key:string]:string
    }
    const style:Style = {
        bg:'w',
        fontSize:'12px'
    };
    const background = color[style.bg];
解决方案二:使用断言
enum color {
        b = 'black',
        w = 'white',
        g = 'green'
    }
    let style= {
        bg:'w'
    };
    const background = color[style.bg as keyof typeof color];


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM