来自 fp-ts 和 URI 的打字稿中更高种类的类型
higher kinded type in typescript from fp-ts and URI
在 fp-ts 中,他们有针对更高级类型的解决方法:
export interface HKT<URI, A> {
readonly _URI: URI;
readonly _A: A;
}
并且可以这样使用:
export interface Foldable<F> {
readonly URI: F;
reduce: <A, B>(fa: HKT<F, A>, b: B, f: (b: B, a: A) => B) => B;
}
有哪些成员_URI
,什么是_A
?
This article written by the author of fp-ts
explains very well,in combination with documentation on TypeScript's union types.
_A
是我们HKT中值的类型:HKT<"Option", string> => Option<string>
。我们稍后会了解映射的工作原理。
_URI
是 HKT 的标识符,例如 Option.ts。它有 2 个实例,None
和 Some
,它们都有 "Option"
作为它们的 _URI
。
Taking a look at the usage of F
in Foldable
我们可以看到它在 Type
类型中使用,以及 A
; Type<F, A>
。假设 F == "Option" & A == string
所以 Type<F, A> == Type<"Option", string>
,这与 Option<string>
非常相似。目前,我们无法将泛型类型传递给另一个泛型,因为它是泛型参数:<F,A>(fa: F<A>)
将无法编译,这是主要问题。
要实现Type<"Option", string> => Option<string>
,有几个“映射”接口(这个在文章中有说明)。我们可以看到 Option
here:
declare module './HKT' {
interface URI2HKT<A> {
Option: Option<A>
}
}
最左边的Option
是字符串key,最右边的是实际的Option
类型,毕竟这是一个普通的记录界面。让我们快速看一下Type
的定义:type Type<URI extends URIS, A> = URI2HKT<A>[URI]
.
URI2HKIT
是从我们的 "Option"
到我们实际的 Option
的映射。它允许传递 2 个泛型参数以取回包含在更高种类类型中的值类型,从而解决前面提到的泛型问题。
在 fp-ts 中,他们有针对更高级类型的解决方法:
export interface HKT<URI, A> {
readonly _URI: URI;
readonly _A: A;
}
并且可以这样使用:
export interface Foldable<F> {
readonly URI: F;
reduce: <A, B>(fa: HKT<F, A>, b: B, f: (b: B, a: A) => B) => B;
}
有哪些成员_URI
,什么是_A
?
This article written by the author of fp-ts
explains very well,in combination with documentation on TypeScript's union types.
_A
是我们HKT中值的类型:HKT<"Option", string> => Option<string>
。我们稍后会了解映射的工作原理。
_URI
是 HKT 的标识符,例如 Option.ts。它有 2 个实例,None
和 Some
,它们都有 "Option"
作为它们的 _URI
。
Taking a look at the usage of F
in Foldable
我们可以看到它在 Type
类型中使用,以及 A
; Type<F, A>
。假设 F == "Option" & A == string
所以 Type<F, A> == Type<"Option", string>
,这与 Option<string>
非常相似。目前,我们无法将泛型类型传递给另一个泛型,因为它是泛型参数:<F,A>(fa: F<A>)
将无法编译,这是主要问题。
要实现Type<"Option", string> => Option<string>
,有几个“映射”接口(这个在文章中有说明)。我们可以看到 Option
here:
declare module './HKT' {
interface URI2HKT<A> {
Option: Option<A>
}
}
最左边的Option
是字符串key,最右边的是实际的Option
类型,毕竟这是一个普通的记录界面。让我们快速看一下Type
的定义:type Type<URI extends URIS, A> = URI2HKT<A>[URI]
.
URI2HKIT
是从我们的 "Option"
到我们实际的 Option
的映射。它允许传递 2 个泛型参数以取回包含在更高种类类型中的值类型,从而解决前面提到的泛型问题。