2013-12-12 2 views
1

에서 인덱싱 가능한 객체 0.9.5로 업데이트 한 후 js-library에 대한 defenition에서 몇 가지 오류를 발견했습니다. 코드를 통해 상황을 설명하겠습니다. 그것은 내 자바 스크립트 코드 :최신 타이프 스크립트 컴파일러 버전 (0.9.5)

var Sample = lib.ClassImpl({ 
    ctor: function(array) { 
     var me = this; 
     this._items = array; 
     this._items.forEach(function(name) { 
      me[name] = new ItemWrapper(name); //Implemetation of ItemWrapper is unnecessary 
     }); 
    }, 
    doSomething: function() { 
     //some logic 
    }, 
    returnSomething: function() { 
     return this._items.length; 
    } 
}); 

그리고 이것은 사용이다 : 나는 할 수 있었다 타이프 라이터 컴파일러의 이전 버전 (0.9.1.1)에 대한 쓴 한 타이프 라이터 defenitions에서

var sample = new Sample(["First", "Second", "Third"]); 
sample.doSomething(); 
console.log(sample.returnSomething()); 
console.log(sample.First.method()); 
console.log(sample.Third.method()); 

(인텔리이 마법처럼 작동)

export interface ISampleBase { 
    doSomething(): void; 
    returnSomething(): number; 
} 
export interface ISample extends ISampleBase { 
    [name: string]: ItemWrapper; //here is the error 
} 
export class Sample implemets ISampleBase { 
    constructor(array: string[]); 
    doSomething(): void; 
    returnSomething(): number; 
} 

을 그리고 내 TS-파일에서 사용 : 할

,369을
var sample: ISample = new Sample(["First", "Second", "Third"]); 
sample.doSomething(); 
sample["Third"].method(); 

그러나 버전 0.9.5로 업데이트 한 후 모든 명명 된 속성은 코멘트로 표시된 줄 위의 문자열 인덱서 유형 ItemWrapper의 하위 유형에 따라야한다는 오류가 있습니다. 문제를 해결할 수있는 변형이 있습니까?

TypeScript playground with example.

+0

그래서 더 이상 합법적이지 않아서 결국 어떻게 됐는지 궁금합니다. d.ts 파일과 비슷한 문제가 있습니다. – Travis

답변

2

는 타이프에서 더 이상이 문제를 표현하는 방법이 없습니다. 이것은 본질적으로 위험한 패턴입니다 (예 : ["doSomething", "returnSomething"]을 해당 생성자에 전달하면 어떻게됩니까?). 색인 생성기가 어떤 의미인지에 대한 규칙의 복잡성을 지원할 가치가 없었습니다.