2015-07-15 3 views
1

Есть ли способ в TypeScript, чтобы анонимировать свойства объектного литерала для типа?Свойства анотирования объекта литерал

Рассмотрим следующий пример:

interface ILiteralType 
{ 
    // I don't know what is going to be part of this object 
    // but I know it should be of type string. 
    *: string; 
} 

class MyClass 
{ 
    properties: ILiteralType; 
} 

var m = new MyClass(); 
m.properties = { 
    name: "A name",   // ok 
    age: 10,     // error: Should be a string 
    anotherProp: "some value" // ok too 
    ... 
}; 

ответ

1

Я думаю, что вы ищете это:

interface ILiteralType 
{ 
    [property: string]: string; 
} 

Это потребует всех значений свойств быть строками.

+0

Да, спасибо :) – Ayman

Смежные вопросы