我正在尝试扩展以下 Next.js 函数类型:
export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = (
context: GetStaticPropsContext<Q, D>
) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>
Run Code Online (Sandbox Code Playgroud)
因此,其上下文 ( GetStaticPropsContext) 和 never上有 3 个属性undefined,如下所示:
type UpdateContext<T> = T extends (context: infer Context extends GetStaticPropsContext) => infer Return
? (context: SelectiveRequiredNotUndefined<Context, "locale" | "locales" | "defaultLocale">) => Return
: never;
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做,我可以使用新类型,其中这些上下文属性永远不会undefined:
type MyGetStaticProps …Run Code Online (Sandbox Code Playgroud) 在fp-ts中,它们对更高种类的类型有以下解决方法:
export interface HKT<URI, A> {
readonly _URI: URI;
readonly _A: A;
}
Run Code Online (Sandbox Code Playgroud)
可以这样使用:
export interface Foldable<F> {
readonly URI: F;
reduce: <A, B>(fa: HKT<F, A>, b: B, f: (b: B, a: A) => B) => B;
}
Run Code Online (Sandbox Code Playgroud)
成员_URI是_A什么?成员是什么?