✅ 操作成功!

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped

发布时间:2023-06-12 作者:admin 来源:文学

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead 写接口类型时,希望有两个类型的键值是联合类型动态需要也需要约束起来,比如: interface ResDataType<T extends string> { // T需要增加 extends string 避免传入其他类型 titleType: T; // titleData: { [key: T]: string }; // 这时候key会报出错误 titleData: { [key in T]: string }; // 正确写法,必须在T上增加继承 string } const res: ResDataType<'edit' | 'new'> = { titleType: 'new', // edit | new titleData: { edit: '编辑', new: '添加', }, } console.log(res.titleData[res.titleType]);

希望能帮到大家,我也是参考

👁️ 阅读量:0