![](/img/trans.png)
[英]Typescript: How to declare the type for a function with custom properties that is returned from an IIFE
[英]How to declare type for function with properties in TypeScript?
我对 TypeScript 有点陌生,并阅读了 TypeScript 网站上的一些文档,但我不确定我是否理解正确。 在这种情况下,我使用 package 没有可用的 TypeScript 类型( prismic-reactjs
),所以我试图自己声明这些类型,以便我可以摆脱困扰我的项目的错误。
因此,作为一个基本示例,我将如何为这样的 function 编写声明:
const foo = ({ bar }) => {
return bar;
};
foo.baz = () => "string";
我可以做到这一点:
declare module "my-module" {
interface fooParams {
bar: string;
}
export function foo({ bar }: fooParams): string;
}
但我不知道如何包含baz
属性。
您可以使用交集类型来实现这一点。
declare const foo: {
baz: () => string
} & (({ bar }: {bar: string}) => string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.