TS2339:类型“ LocalizedStringsMethods”上不存在属性“ reactive_support”

时间:2019-01-03 07:17:52

标签: reactjs typescript

遇到类似TS2339的错误:类型'LocalizedStringsMethods'不存在属性'reactive_support'

将打字稿从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

2 个答案:

答案 0 :(得分:0)

您应遵循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

答案 1 :(得分:0)

使用括号而不是点符号后,它开始工作并能够解决错误。

“标签”:字符串[reactive_support],