메신저 typescipt 2를 사용합니다. Im은 서버에서 모델을 받아서 사용할 수있는 인스턴스로 변환하는 일부 파서 메서드를 작성합니다.Typescript (Angular2) 제네릭 생성자 팩토리
코드:
export interface Constructable {
copy(other : any) : void;
}
//in my Convert class which converts server responses to model
private static _parseT<T extends Constructable>(obj : any) : T {
let instance = Object.create(T); // the row of the compile-time error
instance.constructor.apply(instance);
instance.copy(obj);
return instance;
}
와 내가 컴파일 타임 오류
에게이찾을 수 없습니다 이름 T
export class Foo implements Constructable {
private test : string = null;
public copy(other:any) : void {
this.test = other.test;
}
}
다음과 같은 클래스가 있다고 가정 할 수 있습니다
이제는 구문이 아니라는 것을 확신하지만 어떻게 찾지 못했습니다.
물건을 정리하십시오. useage가 보이는 방법이 있습니다 :
public static ParseFoo(data: any) : Foo{
return Convert._parseT<Foo>(data.mResult); // data is a server response object
}
참고 일부 공장 패턴이이 문제를 해결 것이지만, 난 정말 Copy
방법 대신 만들고 인스턴스를 반환 일부 Generate
와 함께 머물 싶습니다
은 그것의 synatictically 부정확하다. clazz : new (... args : any []) : T가 잘못되었습니다. 새로운 (... args : any []) => T 그리고 나서 컴파일 오류도 발생했습니다. –
아, 죄송 합니다만이 오타는 수정 하겠지만 두 번째 것은 컴파일해야합니다 ... o '(링크 된 예제를 확인해 보면 완벽하게 컴파일됩니다.) – Supamiu
'new '(... args : any []) => T'에 'copy'속성이 없습니다. 'newargs : 어떤 []) => T '는 T 타입에 할당 할 수 없습니다. –