【问题标题】:How can I declare public and private variables in Typescript with interfaces?如何在带有接口的 Typescript 中声明公共和私有变量?
【发布时间】:2014-09-07 19:48:56
【问题描述】:

我有以下代码:

interface IEnumElement {
    elem: string;
    text: string;
    val: number
}
interface IIndexable<T> {
    [index: string]: T;
}

class AdminBase {

    ExamStatusId: IIndexable<IEnumElement> = {
        All: {
            elem: "",
            text: 'Exam Status: All',
            val: 0
        },
        Current: {
            elem: "asdf",
            text: 'Exam Status: Current',
            val: 1
        }
    }
}

这给了我一个错误提示:

Error   5   Public property 'ExamStatusId' of exported class has or is 
using private type 'IEnumElement'.  C:\Protractor\Latest\TypeScript1.ts

【问题讨论】:

    标签: typescript


    【解决方案1】:

    我想问题出现了,一旦你开始导出 AdminBase 类。正如这里很好地解释的那样:

    我们要做的,就是导出接口

    export interface IEnumElement {
        elem: string;
        text: string;
        val: number
    }
    export interface IIndexable<T> {
        [index: string]: T;
    }
    
    export class AdminBase {
    
        ExamStatusId: IIndexable<IEnumElement> = {
            All: {
                elem: "",
                text: 'Exam Status: All',
                val: 0
            },
            Current: {
                elem: "asdf",
                text: 'Exam Status: Current',
                val: 1
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 1970-01-01
      • 2013-06-11
      • 2018-11-12
      • 2013-04-14
      • 2013-08-09
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多