相关疑难解决方法(0)

如何推断 TypeScript 中函数类型的类型参数定义?

我正在尝试扩展以下 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)

typescript typescript-generics

5
推荐指数
1
解决办法
324
查看次数

来自fp-ts和URI的打字稿中的更高种类的类型

在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什么?成员是什么?

functional-programming typescript fp-ts

3
推荐指数
1
解决办法
1075
查看次数