0

나는 현재 타이프 스크립트 (typcript)에서 좀 더 진보 된 타이핑을 사용하고 있으며, 하이퍼 스크립트 (hyperscript)와 같은 함수를 정의하는 방법을 궁금해하고있다. 나는 다양한 접근법을 시도했지만 성공적으로 h 함수를 오버로드 할 수없고 사용법 주석 아래 나열된 모든 CallExpressions을 전달할 수 없습니다. 여기 하이퍼 스크립트 서명을위한 TypeScript 함수 오버로딩

는 내가 지금까지 무엇을 가지고 :

interface IProps { 
    [key: string]: any; 
} 

function h(tag: string, props?: IProps): void; 
function h(tag: string, children: string): void; // <- marked as invalid 
function h(tag: string, props: IProps, children?: string): void { 
    // ...code goes here 
} 

사용법 :

h("div"); 
h("div", "Hello World"); 
h("div", { className: "test" }); 
h("div", { className: "test" }, "Hello World"); // <- marked as invalid 

답변

0

오늘 아침에 깨어 난 후 답을 발견했다. TypeScript에서는 구현 서명이 외부 세계에 표시되지 않으므로 호출식이 오버로드 중 하나와 일치해야합니다.

또한 구현 시그니처의 유형은 이전의 모든 오버로드를 catch해야합니다.

// Overloads, must match one of these 
function h(tag: string, children?: string): void; 
function h(tag: string, props: IProps, children?: string): void; 

// Not visible for the outside world 
function h(tag: string, props: IProps | string, children?: string): void { 
    // ... 
}