{"id":13441664,"url":"https://github.com/danielnixon/total-functions","last_synced_at":"2025-03-20T12:32:14.329Z","repository":{"id":47965286,"uuid":"210476599","full_name":"danielnixon/total-functions","owner":"danielnixon","description":"A collection of total functions to replace TypeScript's built-in partial functions.","archived":true,"fork":false,"pushed_at":"2021-08-11T12:50:03.000Z","size":1673,"stargazers_count":29,"open_issues_count":19,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T14:59:34.153Z","etag":null,"topics":["functional-programming","type-safety","typescript"],"latest_commit_sha":null,"homepage":null,"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/danielnixon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-24T00:25:49.000Z","updated_at":"2023-01-28T06:56:26.000Z","dependencies_parsed_at":"2022-08-12T15:21:04.979Z","dependency_job_id":null,"html_url":"https://github.com/danielnixon/total-functions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielnixon%2Ftotal-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielnixon%2Ftotal-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielnixon%2Ftotal-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielnixon%2Ftotal-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielnixon","download_url":"https://codeload.github.com/danielnixon/total-functions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244611348,"owners_count":20481179,"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":["functional-programming","type-safety","typescript"],"created_at":"2024-07-31T03:01:36.661Z","updated_at":"2025-03-20T12:32:14.323Z","avatar_url":"https://github.com/danielnixon.png","language":"TypeScript","funding_links":[],"categories":["HarmonyOS"],"sub_categories":["Windows Manager"],"readme":"# TypeScript Total Functions\n\n[![Build Status](https://travis-ci.org/danielnixon/total-functions.svg?branch=master)](https://travis-ci.org/danielnixon/total-functions)\n[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage\u0026prefix=%E2%89%A5\u0026suffix=%\u0026query=$.typeCoverage.atLeast\u0026uri=https%3A%2F%2Fraw.githubusercontent.com%2Fdanielnixon%2Ftotal-functions%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)\n[![Known Vulnerabilities](https://snyk.io/test/github/danielnixon/total-functions/badge.svg?targetFile=package.json)](https://snyk.io/test/github/danielnixon/total-functions?targetFile=package.json)\n[![npm](https://img.shields.io/npm/v/total-functions.svg)](https://www.npmjs.com/package/total-functions)\n\n[![dependencies Status](https://david-dm.org/danielnixon/total-functions/status.svg)](https://david-dm.org/danielnixon/total-functions)\n[![devDependencies Status](https://david-dm.org/danielnixon/total-functions/dev-status.svg)](https://david-dm.org/danielnixon/total-functions?type=dev)\n\nA collection of total functions to replace TypeScript's built-in [partial functions](https://wiki.haskell.org/Partial_functions).\n\nIntended to be used with [strictNullChecks](https://www.typescriptlang.org/docs/handbook/compiler-options.html) enabled.\n\n## Installation\n\n```sh\n# yarn\nyarn add total-functions\n\n# npm\nnpm install total-functions\n```\n\n## The Functions\n\n### `get` (type-safe member and indexed access operator)\n\nPrior to TypeScript 4.1's [noUncheckedIndexedAccess](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#no-unchecked-indexed-access) option,  member access for arrays and records was not type safe. For example:\n\n```typescript\nconst a: string[] = [];\nconst b = a[0]; // b has type string, not string | undefined as you might expect\nb.toUpperCase(); // This explodes at runtime\n\nconst record: Record\u003cstring, string\u003e = { foo: \"foo\" };\nconst bar = record[\"bar\"]; // bar has type string, not string | undefined\nbar.toUpperCase(); // This explodes at runtime\n\nconst baz = record.baz; // baz has type string, not string | undefined\nbaz.toUpperCase(); // This explodes at runtime\n\nconst str = \"\";\nconst s = str[0]; // s has type string, not string | undefined\ns.toUpperCase(); // This explodes at runtime\n```\n\n`get` is a safe alternative:\n\n```typescript\nimport { get } from \"total-functions\";\n\nconst b = get(a, 0); // b has type object | undefined\n\nconst bar = get(record, \"bar\"); // bar has type string | undefined\n```\n\nNote that `get` will exclude `undefined` from the return type when there is enough type information to be confident that the result cannot be undefined. See the object and tuple examples below for examples where `undefined` is not included in the return type.\n\nMore usage examples:\n\n```typescript\n// tuple\nconst xs = [1, 2, 3] as const;\nconst x1 = get(xs, 1); // 2\nconst x100 = get(xs, 100); // undefined\nconst xMinus1 = get(xs, -1); // undefined\nxs.map(x =\u003e x /* 1 | 2 | 3 */);\n\n// array\nconst ys = [1, 2, 3];\nconst y1 = get(ys, 1); // number | undefined\nconst y100 = get(ys, 100); // number | undefined\nys.map(y =\u003e y /* number */);\n\n// sparse array\nconst zs = [1, , 2, 3];\nconst z1 = get(zs, 1); // number | undefined\nconst z100 = get(zs, 100); // number | undefined\nzs.map(z =\u003e z /* number | undefined */);\n\n// readonly array\nconst as: ReadonlyArray\u003c1 | 2 | 3\u003e = [1, 2, 3];\nconst a1 = get(as, 1); // 1 | 2 | 3 | undefined\nconst a100 = get(as, 100); // 1 | 2 | 3 | undefined\n\n// record\nconst record: Record\u003cnumber, string\u003e = { 1: \"asdf\" };\nconst record1 = get(record, 1); // string | undefined\nconst record100 = get(record, 100); // string | undefined\n\n// object\nconst obj = { 1: \"asdf\" };\nconst obj1 = get(obj, 1); // string\nconst obj100 = get(obj, 100); // doesn't compile\n\n// const object\nconst constObj = { 1: \"asdf\" } as const;\nconst constObj1 = get(constObj, 1); // \"asdf\"\nconst constObj100 = get(constObj, 100); // doesn't compile\n```\n\nYou only need to use this if you are stuck on Typescript \u003c 4.1.\n\n## ESLint\n\nThere's a corresponding ESLint plugin to enforce the use of `noUncheckedIndexedAccess` and/or ban the partial functions replaced by this library.\n\nSee https://github.com/danielnixon/eslint-plugin-total-functions\n\n# See Also\n* [TypeScript for Functional Programmers](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html)\n* https://github.com/danielnixon/readonly-types\n* https://github.com/danielnixon/eslint-plugin-total-functions\n* https://github.com/jonaskello/eslint-plugin-functional\n* https://github.com/danielnixon/eslint-config-typed-fp\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielnixon%2Ftotal-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielnixon%2Ftotal-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielnixon%2Ftotal-functions/lists"}