2016-07-11 4 views
0

내가 타이프 라이터가 내장 된 인터페이스, ArrayLike 사용하려고 ArrayLike 인터페이스없는,하지만 여전히 다음과 같은 오류 메시지 받고 있습니다 :타이프 라이터 : 인덱스 서명

Error:(12, 11) TS2420: Class 'Point' incorrectly implements interface 'ArrayLike'. Index signature is missing in type 'Point'.

interface ArrayLike<T> { 
    length: number; 
    [n: number]: T; 
} 

class Point implements ArrayLike<number> { 
    [0]: number = 10; 
    length: number = 1; 
} 

가 어떻게이 문제를 해결할 수 있습니까? (또는 다른 해결 방법)

답변

0

인터페이스 ArrayLike 구현하는 방법 : I하지만 여전히

class Point implements ArrayLike<number> { 
    [n: number]: number; 
    length: number = 1; 
    [0]: number = 10; 
} 

let p = new Point() 
p[23] = 34; // ok 
p[23] = "34"; // Error: Type 'string' is not assignable to type 'number' 
+0

이제 나는 내가 놓친 것을 이해합니다. 고맙습니다! – Gloridea

0

Point을 배열로 만들려면 Array 클래스를 확장하는 것이 가장 좋은 이유는 무엇입니까?

class Point extends Array<number> { 
    // add Point array methods here 
} 

이 방법 Point 자동으로 ArrayLike<number> 인터페이스를 구현합니다.

+0

참고로, http://stackoverflow.com/questions/26700164/extending-array-with-es6-classes#28224044 – Paleo

+0

@Paleo 흥미를 'Array' 클래스를 확장 할 때 어떤 문제도 발생하지 않았습니다. 또한 배열의 길이를 직접 설정하려는 이유는 무엇입니까? 그것은 거의 의미가 없습니다. –

+0

감사합니다. 왜'ArrayLike '인터페이스를 사용해야하는지 모르겠습니다. 방금 질문에 대답했습니다. : s – Paleo