두 개의 매개 변수를 사용하는 함수를 만들려고합니다. 첫 번째는 클래스 참조이고 두 번째는 클래스 참조의 인스턴스이고 출력이 특정 유형 인 하나의 매개 변수 만있는 함수입니다. 아래 코드를 시도했지만 p가 Pair의 인스턴스 였으면 좋겠지 만 typescript는 p에 대해 알 수없는 유형을 보여줍니다.
interface Point{
x: number
y: number
}
class Pair{
key = 0
value = 0
}
function set<Class extends { new (): Type }, Type>(
inputClass: Class,
constructor: (o: Type) => Point) {
//implementation...
}
set(Pair, p => ({x: p.key, y: p.value}))
// Typescript says:
// (parameter) p: unknown
// Object is of type 'unknown'.(2571)
나는 p가 Pair의 인스턴스 여야한다는 것을 타이프 스크립트로 알고 싶었습니다.
Java 제네릭을 사용하여 구현할 수 있었지만 여전히 typescript에서 복제 할 수 없습니다. 코드를 따르십시오.
public <K extends Pair, T extends Class<K>> void setRenderer(T classe, Function<K, Point> constructor){
//implementation
}
이것은 작동합니다.
function set<C>(
inputClass: new ()=>C,
constructor: (o: C) => Point) {
//implementation...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다