得到类似TS2339的错误:属性'reactive_support'在类型'LocalizedStringsMethods'上不存在
升级的打字稿从2.6到2.9,并尝试过
import LocalizedStrings from 'react-localization';
const JsonData = require('../LocalizationStrings.json');
interface LocalizedStringsMethods {
reactive_support: string;
}
let strings = new LocalizedStrings(JsonData);
let categories = {
'Out-of-support': { 'label': strings.reactive_support,
'desc': strings.FW_has_no_known_security_bulletins_only_FW_fixes_addressing_prioritized_security_risks,
'style': 'printer-status-out-of-support'
}
};
我正在阅读Json文件,Json文件的名称是LocalizationStrings.json
您应该按照npm页面https://www.npmjs.com/package/react-localization上提供的说明进行操作。这是该页面上打字稿的建议解决方案。
export interface IStrings extends LocalizedStringsMethods{
score:string;
time: String;
}
public strings: IStrings;
this.strings = new LocalizedStrings({
it: {
score: "Punti",
time: "Tempo"
},
en: {
score: "Score",
time: "Time"
}
});
在您的代码中,您必须将接口定义为扩展基本LocalizedStringsMethods
接口并将strings
的类型定义为您自己的接口。按照代码中的示例,你会得到类似的东西
import LocalizedStrings, { LocalizedStringsMethods } from 'react-localization';
const JsonData = require('../LocalizationStrings.json');
interface IStrings extends LocalizedStringsMethods {
reactive_support: string;
FW_has_no_known_security_bulletins_only_FW_fixes_addressing_prioritized_security_risks: string;
}
let strings: IStrings = new LocalizedStrings(JsonData);
let categories = {
'Out-of-support': { 'label': strings.reactive_support,
'desc': strings.FW_has_no_known_security_bulletins_only_FW_fixes_addressing_prioritized_security_risks,
'style': 'printer-status-out-of-support'
}
};
现在strings
是IStrings
类型,有reactive_support
属性。以前它隐含的类型为LocalizedStringsMethods
。
它开始工作,能够在使用括号而不是点符号后解决错误。
'label':字符串[reactive_support],