{"id":13567043,"url":"https://github.com/poteat/hkt-toolbelt","last_synced_at":"2025-05-16T00:00:25.590Z","repository":{"id":61896058,"uuid":"555816742","full_name":"poteat/hkt-toolbelt","owner":"poteat","description":"✨Functional and composable type utilities","archived":false,"fork":false,"pushed_at":"2025-01-18T23:09:28.000Z","size":4614,"stargazers_count":260,"open_issues_count":14,"forks_count":10,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-09T19:03:06.115Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://hkt.code.lol","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/poteat.png","metadata":{"files":{"readme":"docs/readme.chinese.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-22T11:57:28.000Z","updated_at":"2025-04-21T23:00:26.000Z","dependencies_parsed_at":"2022-10-23T06:45:09.643Z","dependency_job_id":"136dd1ae-497b-48f7-a292-3b4e5d048a32","html_url":"https://github.com/poteat/hkt-toolbelt","commit_stats":{"total_commits":962,"total_committers":8,"mean_commits":120.25,"dds":"0.24532224532224534","last_synced_commit":"996780705febd63fd9ab29f72c11b04985524f60"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poteat%2Fhkt-toolbelt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poteat%2Fhkt-toolbelt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poteat%2Fhkt-toolbelt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poteat%2Fhkt-toolbelt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/poteat","download_url":"https://codeload.github.com/poteat/hkt-toolbelt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442854,"owners_count":22071877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:02:22.331Z","updated_at":"2025-05-16T00:00:25.033Z","avatar_url":"https://github.com/poteat.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  [HK-Types Toolbelt]\n\u003c/h1\u003e\n\n\u003cp align= \"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/hkt-toolbelt\"\u003e\n    \u003cimg src=https://img.shields.io/npm/v/hkt-toolbelt?color=green\u003e\n  \u003c/a\u003e\n  \u003cimg src=https://img.shields.io/github/workflow/status/poteat/hkt-toolbelt/build\u003e\n  \u003cimg src=https://img.shields.io/github/repo-size/poteat/hkt-toolbelt\u003e\n  \u003cbr\u003e\n  \u003cimg src=https://img.shields.io/npm/dw/hkt-toolbelt\u003e\n  \u003cimg src=https://img.shields.io/github/license/poteat/hkt-toolbelt\u003e\n  \u003ca href=\"https://code.lol\"\u003e\n    \u003cimg src=https://img.shields.io/badge/blog-code.lol-blue\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\" \u003e\n  \u003ci\u003e函数式（functional）、可谱写（composable）的辅助类（type utilities）\u003c/i\u003e\n\u003c/p\u003e\n\n---\n\n此库旨在提供适用于不同领域的辅助类（utility types）,这些类可以通过高阶类(higher-kinded types)以函数的形式加以映射和组合。\n\n利用此库提供的可谱写（composable）且可高效编译（compile-time efficient）的辅助类，您可以更好的编写可靠且类安全的程序。\n\n我们意在实现对数百种高阶类种（kind categories）的支持，例如 **List**， **Boolean**， **String**， **Function** 等等。除此之外，我们还内置了帮助类谱写（composing type）的类组合接口（combinators）。\n\n## 安装\n\n```bash\n\u003e npm install hkt-toolbelt\n```\n\n## 用法\n\n简而言之，**`hkt-toolbelt`** 可以将以下代码：\n\n```ts\n/**\n * 去掉元组中的非数元素\n */\ntype FilterNum\u003cT extends unknown[]\u003e = T extends [Head, ...Tail]\n  ? Head extends number\n    ? [Head, ...FilterNum\u003cTail\u003e]\n    : FilterNum\u003cTail\u003e\n  : [];\n```\n\n简化成这样：\n\n```ts\nimport { $, List, Conditional } from \"hkt-toolbelt\";\n\ntype FilterNum = List.Filter\u003cConditional.Extends\u003cnumber\u003e\u003e;\n```\n\n您可以通过谱写 **`hkt-toolbelt`** 的高阶原始类让自己编写的复杂类更可读。\n\n### 用$调用高阶类（kind）\n\n您可以通过 `$` 运算符来使用您的高阶类:\n\n```ts\ntype Result = $\u003cFilterNum, [1, \"x\", 2, \"y\", 3]\u003e; // [1, 2, 3]\n```\n\n### 子路径导入\n\n导入也可以以子路径的形式实现。\n\n```ts\nimport { $ } from `hkt-toolbelt`;\nimport { Filter } from `hkt-toolbelt/list`;\nimport { Extends } from `hkt-toolbelt/conditional`;\n```\n\n## HKT 是什么?\n\n\u003e **\u003e HKT 是 higher-kinded type 简称**\n\nTypescript 有两种 _不同_ 的类型结构: “类”和“泛型”。\n\n- **type(类)**: 编译时用来描述值的表达式。\n- **generic(泛型)**: 类似模板的形式参数类，可以通过提供一个或多个实际参数实例化，并解析出类（type）。\n\n泛型在 Typescript 中并非一类对象（first-class citizen）——除非已经提供全部所需的实际参数类，否则它们不可以被直接调用；泛型不能作为参数提供给其他泛型，也不能被返还——这些都是由语言本身的局限导致。\n\n**`hkt-toolbelt` 额外引入两个类型结构:**\n\n- **kind(高阶类)**: 编译时用来描述类（type）的表达式，实际设计成函数的形式，可以接纳类作为参数进行运算。\n- **generic kind(高阶泛型)**: 能够返还高阶类的泛型。\n\n而以类(type)为参数的高阶类（kind）运算，我们通过泛型`$\u003ckind, type\u003e`来实现。\n\n利用高阶类，我们可以写出仅凭泛型无法实现的新类型，譬如对泛用函数的紧缩谱写（narrow composition）。\n\n即便某个类可以通过泛型表达，我们也可以利用高阶类实现更美观、易用的接口。\n\n\u003e **关于术语的使用**\n\u003e 高阶类的英文，严格来说仅使用 **_kind_** 并不正确，不过“higher-kinded type”有点长，所以用 **'kind'** 简略表达.\n\u003e\n\u003e 有时候我们也会用“hk-type”来表达，这样其实更合适。\n\n\u003e **中文译者注**\n\u003e 原文中出现的 higher-kinded type（高阶类）, type function（类函数）, higher-kinded-type function（高阶类函数）等词汇，虽然严格来说存在差别，但多数时候可以理解为相似的概念。\n\n## 帮助指引\n\n我们准备了可以帮助您上手 `hkt-toolbelt` 的资料，其中含有对相关概念和用法的详解。\n\n- **[[自定义高阶类]](./guides/custom-kinds.md)** - 如何自定义新的高阶类?\n- **[[参数限定]](./guides/kind-constraints.md)** - 如何限定高阶类的输入参数?\n- **[[底层编码]](./guides/hk-type-encoding.md)** - 底层编码相关细节.\n\n## 相关或相似的项目\n\n- _此库灵感来源于 [ts-toolbelt](https://www.npmjs.com/package/ts-toolbelt)_\n- _超棒的 TS 学习资源: [type-challenges](https://github.com/type-challenges/type-challenges)_\n- _用于值（value）的辅助函数: [lodash](https://lodash.com)_\n\n## 目录\n\n- [API](#api)\n  - [基础接口](#基础接口)\n    - [$\\\u003cF, X\\\u003e](#f-x)\n    - [$$\\\u003cFX, X\\\u003e](#fx-x)\n    - [Cast\\\u003cA, B\\\u003e](#casta-b)\n  - [布尔（Boolean）类接口](#布尔boolean类接口)\n    - [Boolean.And\\\u003cX\\\u003e](#booleanandx)\n    - [Boolean.Or\\\u003cX\\\u003e](#booleanorx)\n    - [Boolean.Not](#booleannot)\n  - [类组合（Combinator）接口](#类组合combinator接口)\n    - [Combinator.Self](#combinatorself)\n    - [Combinator.ApplySelf](#combinatorapplyself)\n  - [条件判定（Conditional）类接口](#条件判定conditional类接口)\n    - [Conditional.Equals\\\u003cA\\\u003e](#conditionalequalsa)\n    - [Conditional.Extends\\\u003cA\\\u003e](#conditionalextendsa)\n    - [If\\\u003cP, T, E\\\u003e](#ifp-t-e)\n  - [函数（Function）类接口](#函数function类接口)\n    - [Function](#function)\n    - [Function.Constant\\\u003cA\\\u003e](#functionconstanta)\n    - [Function.Identity](#functionidentity)\n  - [高阶类（Kind）接口](#高阶类kind接口)\n    - [Kind\\\u003cF\\\u003e](#kindf)\n    - [Kind.Composable\\\u003cFX\\\u003e](#kindcomposablefx)\n    - [Kind.Compose\\\u003cFX\\\u003e](#kindcomposefx)\n    - [Kind.Pipe\\\u003cFX\\\u003e](#kindpipefx)\n    - [Kind.\\_](#kind_)\n  - [列表（List）类接口](#列表list类接口)\n    - [List.Map\\\u003cF\\\u003e](#listmapf)\n    - [List.Find\\\u003cF\\\u003e](#listfindf)\n    - [List.Filter\\\u003cF\\\u003e](#listfilterf)\n    - [List.Append\\\u003cF\\\u003e](#listappendf)\n    - [List.First\\\u003cT\\\u003e](#listfirstt)\n    - [List.Last\\\u003cT\\\u003e](#listlastt)\n    - [List.Pair\\\u003cT\\\u003e](#listpairt)\n    - [List.Every\\\u003cT\\\u003e](#listeveryt)\n    - [List.Some\\\u003cT\\\u003e](#listsomet)\n    - [List.Reverse\\\u003cT\\\u003e](#listreverset)\n    - [List.IsVariadic](#listisvariadic)\n  - [对象（Object）类接口](#对象object类接口)\n    - [Object.Keys\\\u003cF\\\u003e](#objectkeysf)\n    - [Object.Values\\\u003cF\\\u003e](#objectvaluesf)\n    - [Object.MapKeys\\\u003cF\\\u003e](#objectmapkeysf)\n    - [Object.MapValues\\\u003cF\\\u003e](#objectmapvaluesf)\n    - [Object.DeepMap\\\u003cF\\\u003e](#objectdeepmapf)\n    - [Object.Paths](#objectpaths)\n    - [Object.At\\\u003cK\\\u003e](#objectatk)\n    - [Object.AtPath\\\u003cP\\\u003e](#objectatpathp)\n  - [字串（String）类接口](#字串string类接口)\n    - [String.StartsWith\\\u003cS\\\u003e](#stringstartswiths)\n    - [String.EndsWith\\\u003cS\\\u003e](#stringendswiths)\n    - [String.Includes\\\u003cS\\\u003e](#stringincludess)\n    - [String.Append\\\u003cS\\\u003e](#stringappends)\n    - [String.Prepend\\\u003cS\\\u003e](#stringprepends)\n    - [String.IsTemplate](#stringistemplate)\n    - [String.Join\\\u003cS\\\u003e](#stringjoins)\n    - [String.Split\\\u003cS\\\u003e](#stringsplits)\n    - [String.First](#stringfirst)\n    - [String.Last](#stringlast)\n    - [String.Tail](#stringtail)\n    - [String.Init](#stringinit)\n    - [String.Replace\\\u003cFrom, To\\\u003e](#stringreplacefrom-to)\n    - [String.Reverse](#stringreverse)\n    - [String.IsString](#stringisstring)\n    - [String.ToUpper](#stringtoupper)\n    - [String.ToLower](#stringtolower)\n  - [\"`Type`\"接口](#type接口)\n    - [Type.Display](#typedisplay)\n    - [Type.ValueOf](#typevalueof)\n  - [联合（Union）类接口](#联合union类接口)\n    - [Union.ToIntersection](#uniontointersection)\n    - [Union.ToTuple](#uniontotuple)\n\n# API\n\n此库的高阶类多为柯里化（currying）函数，旨在支持以无参风格（point-free style）实现类谱写。因此，使用类接口（API types）时需要先提供“操作”（operations），再提供操作的目标数据。\n\n同样为了支持柯里化和无参风，所有类函数（type functions），例如 full Kinds，一次只接受 _一个_ 参数。\n\n## 基础接口\n\n### $\u003cF, X\u003e\n\n`$` 操作符用于将类作为参数提供给高阶类函数（higher-kinded-type function），相当于 TypeScript 中的 `F\u003cA\u003e` 。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Append\u003c` world`\u003e, `hello`\u003e; // `hello world`\n```\n\n### $$\u003cFX, X\u003e\n\n`$$` 操作符用于将类作为起始参数提供给一条高阶类通道（pipeline of kinds），它其实是简化同时使用 `$` 和 `Kind.Pipe` 的语法糖，用来免去完整调用后者的麻烦。\n\n`Kind.Pipe` 以从左到右的顺序组合类函数。\n\n@参考 `$`\n@参考 `Kind.Compose`\n\n```ts\nimport { $$, Kind, String } from `hkt-toolbelt`;\n\ntype Result = $$\u003c[String.Append\u003c\" world\"\u003e, String.Append\u003c\"!\"\u003e], \"hello\"\u003e; // \"hello world!\"\n```\n\n### Cast\u003cA, B\u003e\n\n`Cast` 用于将一个类转换成另一个类，相当于 TypeScript 中的 `A as B`，多用于微调。\n\n```ts\nimport { Cast } from `hkt-toolbelt`;\n\ntype Result = Cast\u003c`hello`, string\u003e; // `hello`\n```\n\n## 布尔（Boolean）类接口\n\n### Boolean.And\\\u003cX\u003e\n\n`And` 纳入一个布尔类并返还一个函数，这个函数可以纳入另一个布尔类并返回两个布尔类 `\u0026\u0026` 的结果。\n\n```ts\nimport { $, Boolean } from `hkt-toolbelt`;\n\ntype Result = $\u003cBoolean.And\u003ctrue\u003e, false\u003e; // false\n```\n\n### Boolean.Or\\\u003cX\u003e\n\n`Or` 纳入一个布尔类并返还一个函数，这个函数可以纳入另一个布尔类并返回两个布尔类 `||` 的结果。\n\n```ts\nimport { $, Boolean } from `hkt-toolbelt`;\n\ntype Result = $\u003cBoolean.Or\u003ctrue\u003e, false\u003e; // true\n```\n\n### Boolean.Not\n\n`Not` 纳入一个布尔类并返还相反的布尔类。\n\n```ts\nimport { $, Boolean } from `hkt-toolbelt`;\n\ntype Result = $\u003cBoolean.Not, true\u003e; // false\n```\n\n## 类组合（Combinator）接口\n\n### Combinator.Self\n\n`Self` 返还自身，因而可以利用$无限调用。\n\n```ts\nimport { $, Combinator } from `hkt-toolbelt`;\n\ntype Result = $\u003c$\u003cCombinator.Self, `foo`\u003e, `foo`\u003e; // Combinator.Self\n```\n\n### Combinator.ApplySelf\n\n`ApplySelf` 纳入一个高阶类，并将其作为参数提供给其自身，可用于创建 syntho 递归高阶类。\n\n```ts\nimport { $, Combinator } from `hkt-toolbelt`;\n\ntype Result = $\u003cCombinator.ApplySelf, Function.Identity\u003e; // Function.Identity\n```\n\n## 条件判定（Conditional）类接口\n\n### Conditional.Equals\\\u003cA\u003e\n\n`Equals` 用于检验一个类是否等于另一个类，等同于 TypeScript 中的 `A extends B ? ( B extends A ? true : false ) : false` 。\n\n`Equals` 返还一个高阶类函数（higher-kinded-type function），此函数可以纳入另一个类，然后返还一个布尔类。\n\n```ts\nimport { $, Conditional } from `hkt-toolbelt`;\n\ntype Result = $\u003c$\u003cConditional.Equals, `foo`\u003e, `bar`\u003e; // false\n```\n\n### Conditional.Extends\\\u003cA\u003e\n\n`Extends` 用于检验一个类是否是另一个类的子类，等同于 TypeScript 中的 `A extends B ? true : false` 。\n\n需先提供母类，再提供子类。\n\n`Extends` 返还一个高阶类函数（higher-kinded-type function），此函数可以纳入另一个类，然后返还一个布尔类。\n\n```ts\nimport { $, Conditional } from `hkt-toolbelt`;\n\ntype Result = $\u003c$\u003cConditional.Extends, string\u003e, `bar`\u003e; // true\n```\n\n### If\u003cP, T, E\u003e\n\n`If` 根据条件返还一个类，等同于 TypeScript 中的 `P\u003cX\u003e extends true ? T\u003cX\u003e : E\u003cX\u003e`，不过其以无参风格（point-free style）纳入参数 `X`。\n\n`If` 纳入一个条件函数（predicate），一个真时类，和一个假时类；其返还一个高阶类函数，此函数可纳入一个类并根据判定结果返还对应真/假时类。\n\n```ts\nimport { $, Conditional } from \"hkt-toolbelt\";\n\ntype Result = $\u003c\n  Conditional.If\u003c\n    Conditional.Equals\u003c\"foo\"\u003e,\n    String.Append\u003c\"bar\"\u003e,\n    String.Append\u003c\"baz\"\u003e\n  \u003e,\n  \"foo\"\n\u003e; // \"foobar\"\n```\n\n此高阶类专用于类层面的流程控制。\n\n## 函数（Function）类接口\n\n### Function\n\n`Function` 是所有函数的母类，也即所有函数都是 `Function` 的子类。其并非高阶类且不能直接调用。\n\n### Function.Constant\\\u003cA\u003e\n\n`Constant` 纳入一个类并返还一个函数，该函数可纳入任何类并返还最初纳入的类，即它会忽略后来纳入的类且始终返还最初的类。\n\n```ts\nimport { $, Function } from `hkt-toolbelt`;\n\ntype Result = $\u003c$\u003cFunction.Constant, `foo`\u003e, number\u003e; // `foo`\n```\n\n### Function.Identity\n\n`Identity` 纳入一个类，并在高阶类的层面上返还该类。\n\n```ts\nimport { $, Function } from `hkt-toolbelt`;\n\ntype Result = $\u003cFunction.Identity, `foo`\u003e; // `foo`\n```\n\n## 高阶类（Kind）接口\n\n### Kind\\\u003cF\u003e\n\n`Kind` 代表了可以通过 `$` 将类作为参数进行运算的类函数。\n\n您可以选择给 Kind 提供一个函数类（function type）以提高其内部参数和返还值的优先级，以次创造新的高阶类。\n\n### Kind.Composable\\\u003cFX\u003e\n\n`Composable` 检验一个高阶类元组中的高阶类是否可以组合（composable）。如果元组中高阶类 $N$ 的输出类型是高阶类 $N-1$ 的输入类型的子类型，则该元组中的高阶类可以组合。\n\n```ts\nimport { $, Kind, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cKind.Composable, [String.Append\u003c`bar`\u003e, String.Append\u003c`foo`\u003e]\u003e; // true\n```\n\n### Kind.Compose\\\u003cFX\u003e\n\n`Compose` 可以将一个由高阶类组成的元组组合成一个类函数（type function）。\n\n`Compose` 会检查提供的高阶类元组是否可以组合（composable），且会返回一个高阶类函数（higher-kinded-type function)，此函数可纳入一个类然后返还组合的结果。\n\n`Compose` 从右向左执行函数，即——遵循数学的传统——元组中最后一个函数最先执行。\n\n```ts\nimport { $, Kind, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cKind.Compose\u003c[String.Append\u003c`bar`\u003e, String.Append\u003c`foo`\u003e]\u003e, ``\u003e; // `foobar`\n```\n\n### Kind.Pipe\\\u003cFX\u003e\n\n`Pipe` 纳入一个由类函数（type function）组成的元组，并将它们“输送”到另一个类函数（type function）中。这里的执行顺序是从左向右，即元组中的首个函数最先执行，与 `Compose` 刚好相反。\n\n`Pipe` 对程序员来说更直观，因为其阅读顺序与执行顺序统一。`$$` 语法糖实际运行的就是 `Pipe`。\n\n```ts\nimport { $, Kind, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cKind.Pipe\u003c[String.Append\u003c`foo`\u003e, String.Append\u003c`bar`\u003e]\u003e, ``\u003e; // `foobar`\n```\n\n### Kind.\\_\n\n`_` 是类函数（type function）被调用前临时“占位”的独有类。`Kind._` 由 `$` 调用。\n\n## 列表（List）类接口\n\n### List.Map\\\u003cF\u003e\n\n`Map` 传入一个 `函数类` ，并返回一个接受元组类型的高级类型。它将给定的类型函数应用于元组中的每个元素。\n\n```ts\nimport { $, List, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Map\u003cString.Append\u003c`bar`\u003e\u003e, [`foo`, `baz`]\u003e; // [`foobar`, `bazbar`]\n```\n\n### List.Find\\\u003cF\u003e\n\n`Find` 函数先接受一个 `函数类` ，然后接受一个 `元组` ，并返回 finder 函数返回 `true` 的第一个元组元素。如果不存在这样的元素， `Find` 返回 `never` 。\n\n```ts\nimport { $, List, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Find\u003cString.StartsWith\u003c`foo`\u003e\u003e, [`bar`, `foobar`]\u003e; // `foobar`\n```\n\n### List.Filter\\\u003cF\u003e\n\n`Filter` 传入一个类型函数和一个元组，并按输入元组的顺序返回一个元组，因此只有 Filter 函数返回 `true` 的元素保留在结果元组中。\n\n```ts\nimport { $, List, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Filter\u003cString.StartsWith\u003c`foo`\u003e\u003e, [`bar`, `foobar`]\u003e; // [`foobar`]\n```\n\n### List.Append\\\u003cF\u003e\n\n`Append` 传入一个类型和一个元组，并应用该类型，使其被附加到所提供的元组的末尾。\n\n```ts\nimport { $, List } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Append\u003c`bar`\u003e, [`foo`, `baz`]\u003e; // [`foo`, `baz`, `bar`]\n```\n\n### List.First\\\u003cT\u003e\n\n`First` 传入一个元组，并返回该元组的第一个元素。\n\n```ts\nimport { $, List } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.First, [`foo`, `bar`]\u003e; // `foo`\n```\n\n### List.Last\\\u003cT\u003e\n\n`Last` 传入一个 `元组` ，并返回元组的最后一个元素。在具有可变元素的元组的情况下，可变元素被正确处理，即使它是中缀。\n\n```ts\nimport { $, List } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Last, [`foo`, `bar`, `baz`]\u003e; // `baz`\n```\n\n### List.Pair\\\u003cT\u003e\n\n`Pair` 传入一个 `元组` ，并返回元组的元组，其中每个元组是原始元组的一对元素，按顺序排列。如。'[1, 2, 3] '变成'[[1, 2]，[2, 3]]'。\n\n```ts\nimport { $, List } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Pair, [1, 2, 3]\u003e; // [[1, 2], [2, 3]]\n```\n\n对于可变元组，通过引入联合来表示可变对元素的可能组合来处理可变元素。\n\n### List.Every\\\u003cT\u003e\n\n`Every` 接受一个操作函数类和一个元组，如果元组中的每个元素都满足这个操作函数，则返回 `true` ，否则返回 `false` 。\n\n```ts\nimport { $, List, Conditional } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Every\u003cConditional.Extends\u003cnumber\u003e\u003e, [1, 2, 3]\u003e; // true\n```\n\n### List.Some\\\u003cT\u003e\n\n` Some` 接受一个操作函数类和一个元组，如果元组中至少有一个元素满足操作函数类，则返回' true '，否则返回 ` ` false` 。\n\n```ts\nimport { $, List, Conditional } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Some\u003cConditional.Extends\u003cstring\u003e\u003e, [1, 2, 3]\u003e; // false\n```\n\n### List.Reverse\\\u003cT\u003e\n\nReverse 函数接受一个元组，并返回一个包含倒序元素的元组。\n\n这种类型正确地处理可变元组类型，例如。[1、2、……string[]] \"变成\"[…string[] 2 1]”。\n\n```ts\nimport { $, List } from `hkt-toolbelt`;\n\ntype Result = $\u003cList.Reverse, [1, 2, 3]\u003e; // [3, 2, 1]\n```\n\n### List.IsVariadic\n\n`IsVariadic` 传入一个元组，如果元组是可变的，则返回 `true` ，否则返回 `false` 。\n如果一个元组的长度不确定，我们就认为它是可变的。\n\n```ts\nimport { List } from `hkt-toolbelt`;\n\ntype Result = List.IsVariadic\u003c[1, 2, 3]\u003e; // false\n```\n\n## 对象（Object）类接口\n\n### Object.Keys\\\u003cF\u003e\n\nThe `Keys` function takes in an object type, and returns a tuple of the keys of the object.\n\n```ts\nimport { $, Object } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.Keys, { foo: string; bar: number }\u003e; // [\"foo\", \"bar\"]\n```\n\n### Object.Values\\\u003cF\u003e\n\nThe `Values` function takes in an object type, and returns a tuple of the values of the object.\n\n```ts\nimport { $, Object } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.Values, { foo: string; bar: number }\u003e; // [string, number]\n```\n\n### Object.MapKeys\\\u003cF\u003e\n\nThe `MapKeys` function takes in a type function, and an object type, and returns an object type with the keys of the original object type mapped by the given type function.\n\n```ts\nimport { $, Object, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.MapKeys\u003cString.Append\u003c\"bar\"\u003e\u003e, { foo: string }\u003e; // { foobar: string }\n```\n\n### Object.MapValues\\\u003cF\u003e\n\nThe `MapValues` function takes in a type function, and an object type, and returns an object type with the values of the original object type mapped by the given type function.\n\n```ts\nimport { $, Object, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.MapValues\u003cString.Append\u003c\"bar\"\u003e\u003e, { foo: \"foo\" }\u003e; // { foo: \"foobar\" }\n```\n\n### Object.DeepMap\\\u003cF\u003e\n\nThe `DeepMap` function takes in a type function, and an object type, and returns an object type where every value in the object is mapped by the given type function.\n\n```ts\nimport { $, Object, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003c\n  Object.DeepMap\u003cString.Append\u003c\"bar\"\u003e\u003e,\n  { name: { first: \"foo\"; last: \"bar\" } }\n\u003e; // { name: { first: \"foobar\"; last: \"barbar\" } }\n```\n\n### Object.Paths\n\nThe `Paths` type takes in an object type, and returns a tuple of tuples, where each tuple is a path to a value in the object.\n\n```ts\nimport { Object } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.Paths, { name: { first: \"foo\"; last: \"bar\" } }\u003e; // [[\"name\", \"first\"], [\"name\", \"last\"]]\n```\n\n### Object.At\\\u003cK\u003e\n\nThe `At` function takes in a key, and an object type, and returns the value at the given key in the object.\n\n```ts\nimport { $, Object } from \"hkt-toolbelt\";\n\ntype Result = $\u003cObject.At\u003c\"name\"\u003e, { name: \"foo\" }\u003e; // \"foo\"\n```\n\n### Object.AtPath\\\u003cP\u003e\n\nThe `AtPath` function takes in a path, and an object type, and returns the value at the given path in the object.\n\n```ts\nimport { $, Object } from \"hkt-toolbelt\";\n\ntype Result = $\u003c\n  Object.AtPath\u003c[\"name\", \"first\"]\u003e,\n  { name: { first: \"foo\"; last: \"bar\" } }\n\u003e; // \"foo\"\n```\n\n## 字串（String）类接口\n\n### String.StartsWith\\\u003cS\u003e\n\n`StartsWith` 函数接受一个模版字符串，并返回它是否以给定的前缀开头，根据情况返回 `true` 或 `false` 。\n\n当 _开始于_ 这个开头 `string` ，因此 `StartsWith\u003cstring\u003e` 对于所有后续的字符串类型将返回 true。\n\n然而， `string` 开头没有特定的前缀，因此 `$\u003cstartwith \u003c` f `\u003e, string\u003e` 将导致 false。所有字符串也都以空字符串开头。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.StartsWith\u003c`foo`\u003e, `foobar`\u003e; // true\n```\n\n### String.EndsWith\\\u003cS\u003e\n\n`EndsWith` 传入一个模版字符串，并返回是否以给定后缀结束，根据情况返回 `true` 或 `false` 。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.EndsWith\u003c`bar`\u003e, `foobar`\u003e; // true\n```\n\n### String.Includes\\\u003cS\u003e\n\n`Includes` 传入一个字符串文字并返回它是否包含给定字符串的子字符串，根据情况返回 `true` 或 `false` 。\n\n@see `String.StartsWith`\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Includes\u003c`foo`\u003e, `barfoobar`\u003e; // true\n```\n\n### String.Append\\\u003cS\u003e\n\n`Append` 传入一个模版字符串并返回一个高阶函数类，该函数接受一个字符串并返回将传入的模版字符串添加到字符串末尾的结果。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Append\u003c`bar`\u003e, `foo`\u003e; // `foobar`\n```\n\n### String.Prepend\\\u003cS\u003e\n\n`Prepend` 传入一个模版字符类型并返回一个高阶类型函数，该函数接受一个字符串并返回将传入的模版字符串添加到字符串开头的结果。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Prepend\u003c`foo`\u003e, `bar`\u003e; // `foobar`\n```\n\n### String.IsTemplate\n\n`IsTemplate` 传入一个字符串并返回它是否是模板字符串类型，根据情况返回 `true` 或 `false` 。\n\n如果一个字符串不能被简化为字面值字符串，即如果其中包含 `${string}` ，则该字符串被认为是模板字面值。\n\n\u003e 这可能会是一步非常耗费性能的计算。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.IsTemplate, `foo${string}`\u003e; // true\n```\n\n### String.Join\\\u003cS\u003e\n\n`Join` 传入一个模版字符串，并返回一个更高类型的函数，该函数接受一个字符串元组，并返回以模版字符串值作为分隔符连接元组中的字符串的结果。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Join\u003c` `\u003e, [`foo`, `bar`, `baz`]\u003e; // `foo bar baz`\n```\n\n`Join` 也可以处理模板字符串，并将正确处理模板字面量的嵌入表达式。在可变元组输入的情况下，我们将联接解析为 `string` 。分隔符和元组元素都支持字符串联合。\n\n### String.Split\\\u003cS\u003e\n\n`Split` 传入一个模版字符串值并返回一个更高类型的函数，该函数接受一个字符串并返回一个字符串元组，其中原始字符串根据字符串字面值进行拆分。\n\n```ts\nimport { $, String } from `hkt-toolbelt`;\n\ntype Result = $\u003cString.Split\u003c` `\u003e, `foo bar baz`\u003e; // [`foo`, `bar`, `baz`]\n```\n\n`Split` 也可以处理模板字符串，并将正确处理模板字面量的嵌入表达式。但是，所有字符串字面值分隔符的结果都是 `string[]` 作为分割结果。分隔符和元组元素都支持字符串联合。\n\n### String.First\n\n\u003e **\"If ya ain't `[First]`, you're `[Last]`\"** - _Ricky Bobby_\n\nThe `First` function takes in a string and returns the first character of the string.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.First, \"foo\"\u003e; // \"f\"\n```\n\n### String.Last\n\nThe `Last` function takes in a string and returns the last character of the string.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.Last, \"foo\"\u003e; // \"o\"\n```\n\n### String.Tail\n\nThe `Tail` function takes in a string and returns the string with the first character removed.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.Tail, \"foobar\"\u003e; // \"oobar\"\n```\n\n### String.Init\n\nThe `Init` function takes in a string and returns the string with the last character removed.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.Init, \"foobar\"\u003e; // \"fooba\"\n```\n\n### String.Replace\\\u003cFrom, To\u003e\n\nThe `Replace` generic, given two 'From' and 'To' types that represent a string to replace, and a string to replace it with, returns a higher-kinded-type that takes in a string and returns the result of replacing all instances of the 'From' string with the 'To' string.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.Replace\u003c\"foo\", \"bar\"\u003e, \"foo foo foo\"\u003e; // \"bar bar bar\"\n```\n\n### String.Reverse\n\nThe `Reverse` function takes in a string and returns the string with the characters in reverse order.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.Reverse, \"foobar\"\u003e; // \"raboof\"\n```\n\n### String.IsString\n\nThe `IsString` function takes in a type and returns whether or not it is a string, returning `true` or `false` as appropriate.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.IsString, \"foobar\"\u003e; // true\n```\n\n### String.ToUpper\n\nThe `ToUpper` function takes in a string and returns the string with all characters converted to uppercase.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.ToUpper, \"foobar\"\u003e; // \"FOOBAR\"\n```\n\n### String.ToLower\n\nThe `ToLower` function takes in a string and returns the string with all characters converted to lowercase.\n\n```ts\nimport { $, String } from \"hkt-toolbelt\";\n\ntype Result = $\u003cString.ToLower, \"FOOBAR\"\u003e; // \"foobar\"\n```\n\n## \"`Type`\"接口\n\n### Type.Display\n\nThe `Display` function takes in a type and attempts to force the Typescript compiler to display the resolved type in IDEs and other tools.\n\nThis is a useful internal tool to ensure resultant types remain legible.\n\n```ts\nimport { $, Type } from \"hkt-toolbelt\";\n\ntype Result = $\u003cType.Display, \"foobar\"\u003e; // \"foobar\"\n```\n\n### Type.ValueOf\n\nThe `ValueOf` function takes in a type and returns the associated union value of the type, a higher-kinded equivalent to the `T[keyof T]` operator.\n\n```ts\nimport { $, Type } from \"hkt-toolbelt\";\n\ntype Result = $\u003cType.ValueOf, { foo: \"bar\" }\u003e; // \"bar\"\n```\n\n## 联合（Union）类接口\n\n### Union.ToIntersection\n\nThe `ToIntersection` function takes in a union type and returns the intersection of all the types in the union.\n\n```ts\nimport { $, Union } from \"hkt-toolbelt\";\n\ntype Result = $\u003cUnion.ToIntersection, { foo: \"bar\" } | { bar: \"bar\" }\u003e; // { foo: \"bar\"; bar: \"bar\" }\n```\n\n### Union.ToTuple\n\nThe `ToTuple` function takes in a union type and returns a tuple of all the types in the union.\n\n```ts\nimport { $, Union } from \"hkt-toolbelt\";\n\ntype Result = $\u003cUnion.ToTuple, { foo: \"bar\" } | { bar: \"bar\" }\u003e; // [{ foo: \"bar\" }, { bar: \"bar\" }]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoteat%2Fhkt-toolbelt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpoteat%2Fhkt-toolbelt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoteat%2Fhkt-toolbelt/lists"}