Mogę zdefiniować interfejs dla każdej właściwości obiektu tylko jeden raz?

głosy
0

Oto moja własność, gdzie interfejs definiuje wiele razy:

    ExamStatusId = {
        All: <IEnumElement>{
            id: this.examStatusSelectId,
            text: 'Exam Status: All',
            val: 0
        },
        Current: <IEnumElement>{
            id: this.examStatusSelectId,
            text: 'Exam Status: Current',
            val: 1
        }
    }

Tutaj jest interfejs, który mam:

interface IEnumElement {
    elem: string;
    text: string;
    val: number
}

Czy jest jakiś sposób, że mogę uniknąć konieczności określić interfejs po każdej własności ExamStatusId?

Utwórz 16/07/2014 o 18:20
źródło użytkownik
W innych językach...                            


3 odpowiedzi

głosy
1

Nie obejmowały elemnieruchomości, więc trzeba to zrobić (chociaż to jest styczna).

Moja rada jest taka, aby objąć cud typu wnioskowania, i to zrobić:

interface IEnumElement {
    elem: string;
    text: string;
    val: number
}

class Exam {
    private examStatusSelectId: number;

    ExamStatusId = {
        All: {
            id: this.examStatusSelectId,
            elem: '',
            text: 'Exam Status: All',
            val: 0
        },
        Current: {
            id: this.examStatusSelectId,
            elem: '',
            text: 'Exam Status: Current',
            val: 1
        }
    }
}

// This function must be passed an IEnumElement
function example(element: IEnumElement) {

}

var exam = new Exam();

// The compiler knows this is good
example(exam.ExamStatusId.All);

Jeśli nie zawierają prawidłowych właściwości kiedy ogłoszony ExamStatusIdnieruchomości na swojej klasy, kompilator będzie poprawnie wykryć problem i powiedzieć ci o tym, bo to porównać najlepszego wspólnego typu w ExamStatusIdz IEnumElementinterfejsem (więc nawet jeśli tylko brakowało elemod jednego, to dostaniesz ostrzeżenie.

Odpowiedział 16/07/2014 o 18:56
źródło użytkownik

głosy
1

Tak jest to możliwe z dodatkowym interfejsem jak poniższy kod. Generyczny IIndexable interfejs sprawia, że ​​jest również łatwy w użyciu w różnych miejscach!

interface IEnumElement {
    elem: string;
    text: string;
    val: number
}

interface IIndexable<T> {
    [index: string]: T;
}

var ExamStatusId: IIndexable<IEnumElement>  = {
    All: {
        elem: "",
        text: 'Exam Status: All',
        val: 0
    },
    Current: {
        elem: "asdf",
        text: 'Exam Status: Current',
        val: 1
    }, 
    X:  {} // this is now an error!
}

edit: Zaletą korzystania z interfejsu jest to, że dostaniesz autouzupełnianie też!

Odpowiedział 16/07/2014 o 18:32
źródło użytkownik

głosy
0

Zależy od Twojego celu. Jeśli twoim celem jest zdobycie kontroli intellisense + typ przy dostępie członków obiektu, jak pokazano poniżej:

wprowadzić opis obrazu tutaj

I chcesz sprawdzanie intellisense typu + podczas tworzenia poszczególnych członków:

wprowadzić opis obrazu tutaj

Następnie roztwór masz jest najlepszy mogę myśleć.

Odpowiedział 17/07/2014 o 00:40
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more