0
我目前在打字稿中涉及更高級的打字類型,並且想知道如何定義像來自hyperscript的函數那樣的功能。我嘗試了各種方法,但我無法成功超載h
函數,並使所有使用註釋下列出的CallExpressions都可以通過。用於hyperscript簽名的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