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

问题描述 投票:0回答:2

得到类似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

reactjs typescript
2个回答
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' 
    }
};

现在stringsIStrings类型,有reactive_support属性。以前它隐含的类型为LocalizedStringsMethods


0
投票

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

'label':字符串[reactive_support],

© www.soinside.com 2019 - 2024. All rights reserved.