2016-08-24 3 views
0

typescript에 타입 정의 파일 (.d.ts)을 쓰고 있습니다. "이 객체의 모든 속성은 문자열입니다"라고 지정하려면 다음과 같이하십시오.How do you say이 객체 내의 모든 속성은 문자열입니다

interface IThings { 
    thing: string{} 
} 

그러나 이것은 작동하지 않습니다. 이것을 달성 할 방법이 있습니까? 그런 다음

interface IThings { 
    [name: string]: string; 
} 

:

답변

1

당신은 Indexable Types 사용해야

let a = {} as IThings; 
a["x1"] = "y"; // ok 
a["x2"] = 4; // Type 'number' is not assignable to type 'string' 

(code in playground)

관련 문제