diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 6191febdd..a3e170bc8 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,5 +1,3 @@ github: millsp -custom: https://raw.githubusercontent.com/millsp/ts-toolbelt/master/.github/donate.svg -patreon: pirix -issuehunt: https://issuehunt.io/r/pirix-gh/ts-toolbelt -tidelift: https://tidelift.com/subscription/pkg/npm-ts-toolbelt?utm_source=npm-ts-toolbelt&utm_medium=referral&utm_campaign=readme +custom: https://www.buymeacoffee.com/millsp +patreon: millsp diff --git a/.github/ISSUE_TEMPLATE/---question.md b/.github/ISSUE_TEMPLATE/---question.md deleted file mode 100644 index 2b4e3faaf..000000000 --- a/.github/ISSUE_TEMPLATE/---question.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -name: "🤔 Question" -about: "There's something unclear 🤯" - ---- - -## 🤔 Question - -#### Describe your question - - - -#### Search tags, topics - - - \ No newline at end of file diff --git a/.github/banner.svg b/.github/banner.svg index 2a40812cb..180a85188 100644 --- a/.github/banner.svg +++ b/.github/banner.svg @@ -1,52 +1,59 @@ - + - - - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - + - + diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 4846ecf59..8fd2a98bc 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -3,7 +3,7 @@ * [ ] Used a clear / meaningful title for this pull request * [ ] Tested the changes in your own code (on your projects) -* [ ] Added / Edited tests to reflect changes (`tst` folder) +* [ ] Added / Edited tests to reflect changes (`tests` folder) * [ ] Have read the **Contributing** part of the **Readme** * [ ] Passed `npm test` @@ -21,8 +21,8 @@ * ... #### What tests have you updated? -* tested this in `tst/...` -* tested that in `tst/...` +* tested this in `tests/...` +* tested that in `tests/...` * ... #### Is there any breaking changes? diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb..fb61bc440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,47 @@ +# Changelog + +All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## [9.6.0](https://github.com/millsp/ts-toolbelt/compare/v9.5.1...v9.6.0) (2021-03-10) + + +### Features + +* **paths:** parameter for AutoPath delimiter ([6d5bbdc](https://github.com/millsp/ts-toolbelt/commit/6d5bbdc834ce15dd3339d101c828457b1a34cce4)) + + +### Bug Fixes + +* **f.autopath:** distribute ([29eddd9](https://github.com/millsp/ts-toolbelt/commit/29eddd99c6826696941c50681ce4858243786110)) +* **f.autopath:** do not show primitive props ([1ad2638](https://github.com/millsp/ts-toolbelt/commit/1ad26389aa6a9a795d19d90d2724c73ae2bd51d9)) +* **f.autopath:** no primitve paths ([2bdb477](https://github.com/millsp/ts-toolbelt/commit/2bdb4778325288154c8f71c0a5ae4a1b7e2b217f)) +* **f.narrow:** fix variance and array inference ([4c7618d](https://github.com/millsp/ts-toolbelt/commit/4c7618d77b07d6b8c6dfd5087aa85a073bf57db3)) +* **f.narrow:** higher order function support ([dad05ee](https://github.com/millsp/ts-toolbelt/commit/dad05eec6e6710ea4f63063658a3dee4d9c0ca28)) +* **f.narrow:** preserve fns and better display ([a0d62d8](https://github.com/millsp/ts-toolbelt/commit/a0d62d8cfcd28eab7132c225ba187556ca749b4d)) +* **f.narrow:** preserve functions ([c34f1b9](https://github.com/millsp/ts-toolbelt/commit/c34f1b95d0e0855203104eb696cfcb8221a65374)) +* **f.narrow:** revert variance fixes ([12e014a](https://github.com/millsp/ts-toolbelt/commit/12e014a3f3d34047a3722486a73493f857a3697a)) +* **f.narrow:** variance ([866ecd7](https://github.com/millsp/ts-toolbelt/commit/866ecd76744fc38244d85e7ef7b4bb90105cf7eb)) +* **fn:** allow for curried in compose ([2bc5604](https://github.com/millsp/ts-toolbelt/commit/2bc560446916b423977c25e396b4f1f310b6c03f)) +* **o.paths:** dive within arrays and deeper ([626beb6](https://github.com/millsp/ts-toolbelt/commit/626beb61b3100c5e4fd699c946e0e2fed7e24cb6)) + + +### Others + +* **list:** more flexible keys ([d9415b2](https://github.com/millsp/ts-toolbelt/commit/d9415b2f85633c7c74a815c9909899114faf530c)) +* **o.p:** re-implement ([0f44362](https://github.com/millsp/ts-toolbelt/commit/0f443626dc3b114b6784d2053510a1e0c2f7f839)) +* **release:** 9.5.10 ([1f3928a](https://github.com/millsp/ts-toolbelt/commit/1f3928a70ff2a7e903a5398b53295c9c9997b42c)) +* **release:** 9.5.11 ([bde9210](https://github.com/millsp/ts-toolbelt/commit/bde9210d0a361ddb1e03920a0c2bd42dad17ee30)) +* **release:** 9.5.12 ([80579e1](https://github.com/millsp/ts-toolbelt/commit/80579e1eaa17b2d1c61bd7881d0ea6ff94753141)) +* **release:** 9.5.13 ([277a6e2](https://github.com/millsp/ts-toolbelt/commit/277a6e2ac5ca8be2bbcd7593d987aaac3ea9bb75)) +* **release:** 9.5.2 ([ec4a953](https://github.com/millsp/ts-toolbelt/commit/ec4a953cabe6c4d704c0dca5f37d1dc630de047b)) +* **release:** 9.5.3 ([0836f6e](https://github.com/millsp/ts-toolbelt/commit/0836f6e8187287a0c86f249e4e552d68a44e4f60)) +* **release:** 9.5.4 ([855855e](https://github.com/millsp/ts-toolbelt/commit/855855e520ed4a04059f9d61884d2045dd0d751b)) +* **release:** 9.5.5 ([e28bddf](https://github.com/millsp/ts-toolbelt/commit/e28bddf33066850a764e2ba344883ff84da561b9)) +* **release:** 9.5.6 ([b8e0d0a](https://github.com/millsp/ts-toolbelt/commit/b8e0d0a83228baf666e00f4fdb0d99ca936c133f)) +* **release:** 9.5.7 ([5646455](https://github.com/millsp/ts-toolbelt/commit/564645547862c7d698f4a03b15ccb7a5ffb5fc29)) +* **release:** 9.5.8 ([bceb8be](https://github.com/millsp/ts-toolbelt/commit/bceb8be67701f9c7aba9608aed851567e8118ca5)) +* **release:** 9.5.9 ([533a6ac](https://github.com/millsp/ts-toolbelt/commit/533a6ac0f7b713933c94f0962166c7a0245b055e)) +* **release:** 9.6.0 ([5e6d008](https://github.com/millsp/ts-toolbelt/commit/5e6d008888c1705e41bf752b3a308323a5c563fe)) +* **string:** instantiation limiters ([9a678d8](https://github.com/millsp/ts-toolbelt/commit/9a678d8a400c97436d428dad05405abb154af958)) +* **update:** remove dirty code ([9d0ff64](https://github.com/millsp/ts-toolbelt/commit/9d0ff6441518c2c9a01cea7726c08acd19eb37d9)) +* cleanup ([6f23f2e](https://github.com/millsp/ts-toolbelt/commit/6f23f2ec79145a0545eb45d15d50d0363a119b12)) diff --git a/README.md b/README.md index 31b7b8ded..d0b0c87d3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- banner + banner

TypeScript's largest utility library

@@ -9,15 +9,9 @@ - - - - - Language grade: JavaScript - @@ -50,43 +44,45 @@ · 🍩 Request Feature · - 🤔 Ask Questions + 🤔 Ask Questions

## About -**ts-toolbelt** ships **utility types** that provide simple ways to **update**, **change**, and **compute** TypeScript types. -It offers unique dynamic features, allowing you to write type-safe software more **easily** and in **less time** than you do today. +**ts-toolbelt** is the largest, and most tested type library available right +now, featuring **+200 utilities**. Our type collection packages some of the most +advanced mapped types, conditional types, and recursive types on the market. + +**Spend less time, build stronger**. Benefit from a wide range of generic type +functions to achieve better type safety. + +We work just like lodash, or ramda, but applied to the type system. Our mission +is to provide you with simple ways to compute, change, and create types. We +abstract all those complex type checks away for you. We provide a simple, +reusable, and standard API to help you get more done with TypeScript. -Its programmatic API brings **new capabilities** to TypeScript with **+200** tested type utilities. This makes it the largest and the most reliable type collection out there. -It uses the type system itself for TypeScript to compute complex types. So its API exposes types that trade **CPU & RAM** for higher type safety. +**ts-toolbelt** is a well organized package that can help you perform advanced +operations on object types, union types, as well as function, and literal types. +It is carefully and coherently designed for building robust, flexible, and +type-safe software.

- banner + demo

-### Goals +We are a community and a knowledge base. Everyone is welcome to ask questions +about types. If you are stuck or you misunderstand something, you came to the +right place!. We welcome beginners and advanced developers to come take part. +**Welcome!** -* Answer the question to **"How can I do this in TypeScript?"** -* Provide a programmatic **standard API** for the type system -* Promote type **evolution**/**reusability** within your codebase -* Software that is more **type-safe**, **flexible**, and **robust** -* Bring a whole new set of extra **features** to TypeScript -* Extensively tested type utilities for maximum type safety -* This package aims to be the home of all **utility types** -* [Answer questions about types and share knowledge](#good-to-know-) - -You'll find all the types you can ever need in this single and **well organized** package. ## Getting Started ### Prerequisites -Lowest TypeScript [support](#compatibility) starts at v3.5 - ```sh -npm install typescript@^3.8.0 --save-dev +npm install typescript@^4.1.0 --save-dev ``` For best results, add this to your `tsconfig.json` @@ -99,6 +95,9 @@ For best results, add this to your `tsconfig.json` // this is optional, but enable whenever possible "strict": true, + + // this is the lowest supported standard library + "lib": ["es2015"], } } ``` @@ -120,13 +119,12 @@ type merge = Object.Merge<{name: string}, {age?: number}> // {name: string, age?: number} // Make a field of an `object` optional -type optional = Object.Optional<{id: number, name: string}, "name"}> +type optional = Object.Optional<{id: number, name: string}, "name"> // {id: number, name?: string} ``` -> **`TIP`** You can also grab the demo [over here](https://gist.github.com/millsp/f7f7d18773f79bf0618fb5cd55bd48f8). - -You can [**level-up, and re-code this library from scratch**](https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab). +You can [**level-up, and re-code this library from +scratch**](https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab). ## [Documentation ⤢](https://millsp.github.io/ts-toolbelt/) @@ -134,10 +132,9 @@ You can [**level-up, and re-code this library from scratch**](https://medium.com The project is organized around TypeScript's main concepts: -| | | | | | | -|------------|-------------|------------------|--------------|---------------|----------| -| **A**ny | **B**oolean | **C**lass | **F**unction | **I**teration | **L**ist | -| **N**umber | **O**bject | **O**bject.**P** | **S**tring | **U**nion | Test | +| **Any** | **Boolean** | **Class** | **Function** | **Iteration** | **List** | +|------------|-------------|--------------|--------------|---------------|----------| +| **Number** | **Object** | **Object.P** | **String** | **Union** | **Test** | > **`TIP`** How to choose categories? Match your type with them. @@ -163,23 +160,96 @@ You can also import our non-official API from the community: import {Community} from "ts-toolbelt" ``` -> **`TIP`** The community API is for our community to publish useful types that don't see fit in the standard API. - -### Internal Docs - -If you're interested to **learn** how the internals work, [this tutorial](https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab) will get you on track to start writing your own types. +> **`TIP`** The community API is for our community to publish useful types that +> don't see fit in the standard API. + +### Utility Index + +|ANY|OBJECT|LIST|FUNCTION|STRING|UNION|CLASS|BOOLEAN|NUMBER|OBJECT.P|ITERATION| +|---|---|---|---|---|---|---|---|---|---|---| +|[Await](https://millsp.github.io/ts-toolbelt/modules/any_await.html)|[Assign](https://millsp.github.io/ts-toolbelt/modules/object_assign.html)|[Append](https://millsp.github.io/ts-toolbelt/modules/list_append.html)|[AutoPath](https://millsp.github.io/ts-toolbelt/modules/function_autopath.html)|[At](https://millsp.github.io/ts-toolbelt/modules/string_at.html)|[Diff](https://millsp.github.io/ts-toolbelt/modules/union_diff.html)|[Class](https://millsp.github.io/ts-toolbelt/modules/class_class.html)|[And](https://millsp.github.io/ts-toolbelt/modules/boolean_and.html)|[Absolute](https://millsp.github.io/ts-toolbelt/modules/number_absolute.html)|[Merge](https://millsp.github.io/ts-toolbelt/modules/object_p_merge.html)|[Iteration](https://millsp.github.io/ts-toolbelt/modules/iteration_iteration.html)| +|[At](https://millsp.github.io/ts-toolbelt/modules/any_at.html)|[AtLeast](https://millsp.github.io/ts-toolbelt/modules/object_atleast.html)|[Assign](https://millsp.github.io/ts-toolbelt/modules/list_assign.html)|[Compose](https://millsp.github.io/ts-toolbelt/modules/function_compose.html)|[Join](https://millsp.github.io/ts-toolbelt/modules/string_join.html)|[Exclude](https://millsp.github.io/ts-toolbelt/modules/union_exclude.html)|[Instance](https://millsp.github.io/ts-toolbelt/modules/class_instance.html)|[Not](https://millsp.github.io/ts-toolbelt/modules/boolean_not.html)|[Add](https://millsp.github.io/ts-toolbelt/modules/number_add.html)|[Omit](https://millsp.github.io/ts-toolbelt/modules/object_p_omit.html)|[IterationOf](https://millsp.github.io/ts-toolbelt/modules/iteration_iterationof.html)| +|[Cast](https://millsp.github.io/ts-toolbelt/modules/any_cast.html)|[Compulsory](https://millsp.github.io/ts-toolbelt/modules/object_compulsory.html)|[AtLeast](https://millsp.github.io/ts-toolbelt/modules/list_atleast.html)|[Curry](https://millsp.github.io/ts-toolbelt/modules/function_curry.html)|[Length](https://millsp.github.io/ts-toolbelt/modules/string_length.html)|[Filter](https://millsp.github.io/ts-toolbelt/modules/union_filter.html)|[Parameters](https://millsp.github.io/ts-toolbelt/modules/class_parameters.html)|[Or](https://millsp.github.io/ts-toolbelt/modules/boolean_or.html)|[Greater](https://millsp.github.io/ts-toolbelt/modules/number_greater.html)|[Pick](https://millsp.github.io/ts-toolbelt/modules/object_p_pick.html)|[Key](https://millsp.github.io/ts-toolbelt/modules/iteration_key.html)| +|[Compute](https://millsp.github.io/ts-toolbelt/modules/any_compute.html)|[CompulsoryKeys](https://millsp.github.io/ts-toolbelt/modules/object_compulsorykeys.html)|[Compulsory](https://millsp.github.io/ts-toolbelt/modules/list_compulsory.html)|[Exact](https://millsp.github.io/ts-toolbelt/modules/function_exact.html)|[Replace](https://millsp.github.io/ts-toolbelt/modules/string_replace.html)|[Has](https://millsp.github.io/ts-toolbelt/modules/union_has.html)||[Xor](https://millsp.github.io/ts-toolbelt/modules/boolean_xor.html)|[GreaterEq](https://millsp.github.io/ts-toolbelt/modules/number_greatereq.html)|[Readonly](https://millsp.github.io/ts-toolbelt/modules/object_p_readonly.html)|[Next](https://millsp.github.io/ts-toolbelt/modules/iteration_next.html)| +|[Contains](https://millsp.github.io/ts-toolbelt/modules/any_contains.html)|[Diff](https://millsp.github.io/ts-toolbelt/modules/object_diff.html)|[CompulsoryKeys](https://millsp.github.io/ts-toolbelt/modules/list_compulsorykeys.html)|[Function](https://millsp.github.io/ts-toolbelt/modules/function_function.html)|[Split](https://millsp.github.io/ts-toolbelt/modules/string_split.html)|[IntersectOf](https://millsp.github.io/ts-toolbelt/modules/union_intersectof.html)|||[IsNegative](https://millsp.github.io/ts-toolbelt/modules/number_isnegative.html)|[Update](https://millsp.github.io/ts-toolbelt/modules/object_p_update.html)|[Pos](https://millsp.github.io/ts-toolbelt/modules/iteration_pos.html)| +|[Equals](https://millsp.github.io/ts-toolbelt/modules/any_equals.html)|[Either](https://millsp.github.io/ts-toolbelt/modules/object_either.html)|[Concat](https://millsp.github.io/ts-toolbelt/modules/list_concat.html)|[Length](https://millsp.github.io/ts-toolbelt/modules/function_length.html)||[Last](https://millsp.github.io/ts-toolbelt/modules/union_last.html)|||[IsPositive](https://millsp.github.io/ts-toolbelt/modules/number_ispositive.html)|[Record](https://millsp.github.io/ts-toolbelt/modules/object_p_record.html)|[Prev](https://millsp.github.io/ts-toolbelt/modules/iteration_prev.html)| +|[Extends](https://millsp.github.io/ts-toolbelt/modules/any_extends.html)|[Exclude](https://millsp.github.io/ts-toolbelt/modules/object_exclude.html)|[Diff](https://millsp.github.io/ts-toolbelt/modules/list_diff.html)|[Narrow](https://millsp.github.io/ts-toolbelt/modules/function_narrow.html)||[Merge](https://millsp.github.io/ts-toolbelt/modules/union_merge.html)|||[IsZero](https://millsp.github.io/ts-toolbelt/modules/number_iszero.html)||| +|[Key](https://millsp.github.io/ts-toolbelt/modules/any_key.html)|[ExcludeKeys](https://millsp.github.io/ts-toolbelt/modules/object_excludekeys.html)|[Drop](https://millsp.github.io/ts-toolbelt/modules/list_drop.html)|[NoInfer](https://millsp.github.io/ts-toolbelt/modules/function_noinfer.html)||[NonNullable](https://millsp.github.io/ts-toolbelt/modules/union_nonnullable.html)|||[Lower](https://millsp.github.io/ts-toolbelt/modules/number_lower.html)||| +|[Keys](https://millsp.github.io/ts-toolbelt/modules/any_keys.html)|[Filter](https://millsp.github.io/ts-toolbelt/modules/object_filter.html)|[Either](https://millsp.github.io/ts-toolbelt/modules/list_either.html)|[Parameters](https://millsp.github.io/ts-toolbelt/modules/function_parameters.html)||[Nullable](https://millsp.github.io/ts-toolbelt/modules/union_nullable.html)|||[LowerEq](https://millsp.github.io/ts-toolbelt/modules/number_lowereq.html)||| +|[KnownKeys](https://millsp.github.io/ts-toolbelt/modules/any_knownkeys.html)|[FilterKeys](https://millsp.github.io/ts-toolbelt/modules/object_filterkeys.html)|[Exclude](https://millsp.github.io/ts-toolbelt/modules/list_exclude.html)|[Pipe](https://millsp.github.io/ts-toolbelt/modules/function_pipe.html)||[Pop](https://millsp.github.io/ts-toolbelt/modules/union_pop.html)|||[Negate](https://millsp.github.io/ts-toolbelt/modules/number_negate.html)||| +|[Is](https://millsp.github.io/ts-toolbelt/modules/any_is.html)|[Has](https://millsp.github.io/ts-toolbelt/modules/object_has.html)|[ExcludeKeys](https://millsp.github.io/ts-toolbelt/modules/list_excludekeys.html)|[Promisify](https://millsp.github.io/ts-toolbelt/modules/function_promisify.html)||[Replace](https://millsp.github.io/ts-toolbelt/modules/union_replace.html)|||[Range](https://millsp.github.io/ts-toolbelt/modules/number_range.html)||| +|[Promise](https://millsp.github.io/ts-toolbelt/modules/any_promise.html)|[HasPath](https://millsp.github.io/ts-toolbelt/modules/object_haspath.html)|[Extract](https://millsp.github.io/ts-toolbelt/modules/list_extract.html)|[Return](https://millsp.github.io/ts-toolbelt/modules/function_return.html)||[Select](https://millsp.github.io/ts-toolbelt/modules/union_select.html)|||[Sub](https://millsp.github.io/ts-toolbelt/modules/number_sub.html)||| +|[Try](https://millsp.github.io/ts-toolbelt/modules/any_try.html)|[Includes](https://millsp.github.io/ts-toolbelt/modules/object_includes.html)|[Filter](https://millsp.github.io/ts-toolbelt/modules/list_filter.html)|[UnCurry](https://millsp.github.io/ts-toolbelt/modules/function_uncurry.html)||[Strict](https://millsp.github.io/ts-toolbelt/modules/union_strict.html)|||||| +|[Type](https://millsp.github.io/ts-toolbelt/modules/any_type.html)|[Intersect](https://millsp.github.io/ts-toolbelt/modules/object_intersect.html)|[FilterKeys](https://millsp.github.io/ts-toolbelt/modules/list_filterkeys.html)|[ValidPath](https://millsp.github.io/ts-toolbelt/modules/function_validpath.html)||[ListOf](https://millsp.github.io/ts-toolbelt/modules/union_listof.html)|||||| +|[x](https://millsp.github.io/ts-toolbelt/modules/any_x.html)|[IntersectKeys](https://millsp.github.io/ts-toolbelt/modules/object_intersectkeys.html)|[Flatten](https://millsp.github.io/ts-toolbelt/modules/list_flatten.html)||||||||| +||[Invert](https://millsp.github.io/ts-toolbelt/modules/object_invert.html)|[Group](https://millsp.github.io/ts-toolbelt/modules/list_group.html)||||||||| +||[ListOf](https://millsp.github.io/ts-toolbelt/modules/object_listof.html)|[Has](https://millsp.github.io/ts-toolbelt/modules/list_has.html)||||||||| +||[Merge](https://millsp.github.io/ts-toolbelt/modules/object_merge.html)|[HasPath](https://millsp.github.io/ts-toolbelt/modules/list_haspath.html)||||||||| +||[MergeAll](https://millsp.github.io/ts-toolbelt/modules/object_mergeall.html)|[Head](https://millsp.github.io/ts-toolbelt/modules/list_head.html)||||||||| +||[Modify](https://millsp.github.io/ts-toolbelt/modules/object_modify.html)|[Includes](https://millsp.github.io/ts-toolbelt/modules/list_includes.html)||||||||| +||[NonNullable](https://millsp.github.io/ts-toolbelt/modules/object_nonnullable.html)|[Intersect](https://millsp.github.io/ts-toolbelt/modules/list_intersect.html)||||||||| +||[NonNullableKeys](https://millsp.github.io/ts-toolbelt/modules/object_nonnullablekeys.html)|[IntersectKeys](https://millsp.github.io/ts-toolbelt/modules/list_intersectkeys.html)||||||||| +||[Nullable](https://millsp.github.io/ts-toolbelt/modules/object_nullable.html)|[KeySet](https://millsp.github.io/ts-toolbelt/modules/list_keyset.html)||||||||| +||[NullableKeys](https://millsp.github.io/ts-toolbelt/modules/object_nullablekeys.html)|[Last](https://millsp.github.io/ts-toolbelt/modules/list_last.html)||||||||| +||[Object](https://millsp.github.io/ts-toolbelt/modules/object_object.html)|[LastKey](https://millsp.github.io/ts-toolbelt/modules/list_lastkey.html)||||||||| +||[Omit](https://millsp.github.io/ts-toolbelt/modules/object_omit.html)|[Length](https://millsp.github.io/ts-toolbelt/modules/list_length.html)||||||||| +||[Optional](https://millsp.github.io/ts-toolbelt/modules/object_optional.html)|[List](https://millsp.github.io/ts-toolbelt/modules/list_list.html)||||||||| +||[OptionalKeys](https://millsp.github.io/ts-toolbelt/modules/object_optionalkeys.html)|[Longest](https://millsp.github.io/ts-toolbelt/modules/list_longest.html)||||||||| +||[Overwrite](https://millsp.github.io/ts-toolbelt/modules/object_overwrite.html)|[Merge](https://millsp.github.io/ts-toolbelt/modules/list_merge.html)||||||||| +||[Partial](https://millsp.github.io/ts-toolbelt/modules/object_partial.html)|[MergeAll](https://millsp.github.io/ts-toolbelt/modules/list_mergeall.html)||||||||| +||[Patch](https://millsp.github.io/ts-toolbelt/modules/object_patch.html)|[Modify](https://millsp.github.io/ts-toolbelt/modules/list_modify.html)||||||||| +||[PatchAll](https://millsp.github.io/ts-toolbelt/modules/object_patchall.html)|[NonNullable](https://millsp.github.io/ts-toolbelt/modules/list_nonnullable.html)||||||||| +||[Path](https://millsp.github.io/ts-toolbelt/modules/object_path.html)|[NonNullableKeys](https://millsp.github.io/ts-toolbelt/modules/list_nonnullablekeys.html)||||||||| +||[Paths](https://millsp.github.io/ts-toolbelt/modules/object_paths.html)|[Nullable](https://millsp.github.io/ts-toolbelt/modules/list_nullable.html)||||||||| +||[Pick](https://millsp.github.io/ts-toolbelt/modules/object_pick.html)|[NullableKeys](https://millsp.github.io/ts-toolbelt/modules/list_nullablekeys.html)||||||||| +||[Readonly](https://millsp.github.io/ts-toolbelt/modules/object_readonly.html)|[ObjectOf](https://millsp.github.io/ts-toolbelt/modules/list_objectof.html)||||||||| +||[ReadonlyKeys](https://millsp.github.io/ts-toolbelt/modules/object_readonlykeys.html)|[Omit](https://millsp.github.io/ts-toolbelt/modules/list_omit.html)||||||||| +||[Record](https://millsp.github.io/ts-toolbelt/modules/object_record.html)|[Optional](https://millsp.github.io/ts-toolbelt/modules/list_optional.html)||||||||| +||[Replace](https://millsp.github.io/ts-toolbelt/modules/object_replace.html)|[OptionalKeys](https://millsp.github.io/ts-toolbelt/modules/list_optionalkeys.html)||||||||| +||[Required](https://millsp.github.io/ts-toolbelt/modules/object_required.html)|[Overwrite](https://millsp.github.io/ts-toolbelt/modules/list_overwrite.html)||||||||| +||[RequiredKeys](https://millsp.github.io/ts-toolbelt/modules/object_requiredkeys.html)|[Partial](https://millsp.github.io/ts-toolbelt/modules/list_partial.html)||||||||| +||[Select](https://millsp.github.io/ts-toolbelt/modules/object_select.html)|[Patch](https://millsp.github.io/ts-toolbelt/modules/list_patch.html)||||||||| +||[SelectKeys](https://millsp.github.io/ts-toolbelt/modules/object_selectkeys.html)|[PatchAll](https://millsp.github.io/ts-toolbelt/modules/list_patchall.html)||||||||| +||[Undefinable](https://millsp.github.io/ts-toolbelt/modules/object_undefinable.html)|[Path](https://millsp.github.io/ts-toolbelt/modules/list_path.html)||||||||| +||[UndefinableKeys](https://millsp.github.io/ts-toolbelt/modules/object_undefinablekeys.html)|[Paths](https://millsp.github.io/ts-toolbelt/modules/list_paths.html)||||||||| +||[Unionize](https://millsp.github.io/ts-toolbelt/modules/object_unionize.html)|[Pick](https://millsp.github.io/ts-toolbelt/modules/list_pick.html)||||||||| +||[UnionOf](https://millsp.github.io/ts-toolbelt/modules/object_unionof.html)|[Pop](https://millsp.github.io/ts-toolbelt/modules/list_pop.html)||||||||| +||[Update](https://millsp.github.io/ts-toolbelt/modules/object_update.html)|[Prepend](https://millsp.github.io/ts-toolbelt/modules/list_prepend.html)||||||||| +||[Writable](https://millsp.github.io/ts-toolbelt/modules/object_writable.html)|[Readonly](https://millsp.github.io/ts-toolbelt/modules/list_readonly.html)||||||||| +||[WritableKeys](https://millsp.github.io/ts-toolbelt/modules/object_writablekeys.html)|[ReadonlyKeys](https://millsp.github.io/ts-toolbelt/modules/list_readonlykeys.html)||||||||| +|||[Remove](https://millsp.github.io/ts-toolbelt/modules/list_remove.html)||||||||| +|||[Repeat](https://millsp.github.io/ts-toolbelt/modules/list_repeat.html)||||||||| +|||[Replace](https://millsp.github.io/ts-toolbelt/modules/list_replace.html)||||||||| +|||[Required](https://millsp.github.io/ts-toolbelt/modules/list_required.html)||||||||| +|||[RequiredKeys](https://millsp.github.io/ts-toolbelt/modules/list_requiredkeys.html)||||||||| +|||[Reverse](https://millsp.github.io/ts-toolbelt/modules/list_reverse.html)||||||||| +|||[Select](https://millsp.github.io/ts-toolbelt/modules/list_select.html)||||||||| +|||[SelectKeys](https://millsp.github.io/ts-toolbelt/modules/list_selectkeys.html)||||||||| +|||[Shortest](https://millsp.github.io/ts-toolbelt/modules/list_shortest.html)||||||||| +|||[Tail](https://millsp.github.io/ts-toolbelt/modules/list_tail.html)||||||||| +|||[Take](https://millsp.github.io/ts-toolbelt/modules/list_take.html)||||||||| +|||[Undefinable](https://millsp.github.io/ts-toolbelt/modules/list_undefinable.html)||||||||| +|||[UndefinableKeys](https://millsp.github.io/ts-toolbelt/modules/list_undefinablekeys.html)||||||||| +|||[Unionize](https://millsp.github.io/ts-toolbelt/modules/list_unionize.html)||||||||| +|||[UnionOf](https://millsp.github.io/ts-toolbelt/modules/list_unionof.html)||||||||| +|||[UnNest](https://millsp.github.io/ts-toolbelt/modules/list_unnest.html)||||||||| +|||[Update](https://millsp.github.io/ts-toolbelt/modules/list_update.html)||||||||| +|||[Writable](https://millsp.github.io/ts-toolbelt/modules/list_writable.html)||||||||| +|||[WritableKeys](https://millsp.github.io/ts-toolbelt/modules/list_writablekeys.html)||||||||| +|||[Zip](https://millsp.github.io/ts-toolbelt/modules/list_zip.html)||||||||| +|||[ZipObj](https://millsp.github.io/ts-toolbelt/modules/list_zipobj.html)||||||||| ### [Archives ⤢](https://github.com/millsp/ts-toolbelt/tree/gh-pages) -Access older docs at https://millsp.github.io/ts-toolbelt/version/ - > **`EXAMPLE`** https://millsp.github.io/ts-toolbelt/4.2.1/ -## [Good to Know ⤢](https://github.com/millsp/ts-toolbelt/discussions/q-a) +## [Good to Know ⤢](https://github.com/millsp/ts-toolbelt/discussions/categories/q-a) -In this wiki, you will find some extra resources for your learning, and understanding. +In this wiki, you will find some extra resources for your learning, and +understanding. -**Are you missing something?** Participate to the open-wiki by [posting your questions](https://github.com/millsp/ts-toolbelt/discussions/new). +**Are you missing something?** Participate to the open-wiki by [posting your +questions](https://github.com/millsp/ts-toolbelt/discussions/new). ## Running tests @@ -201,37 +271,35 @@ import {Number, Test} from "ts-toolbelt" const {checks, check} = Test checks([ - check, "31", Test.Pass>(), - check, "2", Test.Pass>(), + check, 31, Test.Pass>(), + check, 2, Test.Pass>(), ]) ``` -> **`TIP`** Place it in a file that won't be executed, it's just for TypeScript to test types. +> **`TIP`** Place it in a file that won't be executed, it's just for TypeScript +> to test types. ### Continuous Integration -The releases are done with Travis CI in stages & whenever a branch or PR is pushed: +The releases are done with Travis CI in stages & whenever a branch or PR is +pushed: - Tests are run with `npm test` -- Tests against [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/) +- Tests against + [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/) - Releases to npm@[branch-name] -If you wrote tests & would like your project to be tested too, please open an issue. - ## Compatibility -The project is maintained to adapt to the constant [changes of TypeScript](https://github.com/Microsoft/TypeScript/wiki/Roadmap): +The project is maintained to adapt to the constant [changes of +TypeScript](https://github.com/Microsoft/TypeScript/wiki/Roadmap): | ts-toolbelt | typescript | |-------------|------------| -| 8.x.x | ^4.0.x | -| 6.x.x | ^3.7.x | -| 4.x.x | ^3.5.x | -| 2.x.x | ^3.5.x | -| 3.x.x | ^3.5.x | -| 1.x.x | ~3.5.x | +| 9.x.x | ^4.1.x | Major version numbers will upgrade whenever TypeScript had breaking changes. + Otherwise, the release versions will naturally follow the semantic versioning. ## What's next @@ -244,11 +312,7 @@ Otherwise, the release versions will naturally follow the semantic versioning. * Need to write more examples -## Related Projects - -* [`eledoc`](https://github.com/millsp/eledoc) - 🌒 A material dark theme for TypeDoc -* [`utility-types`](https://github.com/piotrwitek/utility-types) - Collection of utility types, complementing TypeScript built-in mapped types and aliases - ## License -[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpirix-gh%2Fts-toolbelt.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpirix-gh%2Fts-toolbelt?ref=badge_large) +[![FOSSA +Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpirix-gh%2Fts-toolbelt.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpirix-gh%2Fts-toolbelt?ref=badge_large) diff --git a/package.json b/package.json index 6b9222756..b62f68659 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "ts-toolbelt", - "version": "8.5.2", - "descriptsiption": "Get the most out of TypeScriptsipt", + "version": "9.6.0", + "description": "TypeScript's largest utility library", "keywords": [ "safe", "tools", "types", "typesafe", - "typescriptsipt" + "typescript" ], "homepage": "https://github.com/millsp/ts-toolbelt", "repository": { @@ -43,24 +43,22 @@ "test:lint": "bash scripts/test/lint.sh", "test:types": "bash scripts/test/types.sh" }, - "dependencies": { - "@ui-js/grok": "^1.1.0" - }, + "dependencies": {}, "devDependencies": { - "@commitlint/cli": "^8.3.0", - "@commitlint/config-conventional": "^8.3.0", - "@typescript-eslint/parser": "^3.9.1", + "@commitlint/cli": "^11.0.0", + "@commitlint/config-conventional": "^11.0.0", + "@typescript-eslint/parser": "^4.14.0", "eledoc": "^0.2.0", - "eslint": "^7.0.0", + "eslint": "^7.18.0", "eslint-plugin-fp": "^2.3.0", - "eslint-plugin-react": "^7.20.0", - "husky": "^4.2.5", + "eslint-plugin-react": "^7.22.0", + "husky": "^4.3.0", "npx": "^10.2.0", - "sort-package-json": "^1.42.0", - "standard-version": "^8.0.0", - "ts-node": "^8.10.0", - "tslib": "^2.0.0", - "typedoc": "^0.17.0", - "typescript": "next" + "sort-package-json": "^1.48.0", + "standard-version": "^9.1.0", + "ts-node": "^9.1.0", + "tslib": "^2.1.0", + "typedoc": "^0.20.0", + "typescript": "latest" } } diff --git a/scripts/build/docs.sh b/scripts/build/docs.sh index 9e936aa59..9f7f9ec72 100644 --- a/scripts/build/docs.sh +++ b/scripts/build/docs.sh @@ -11,7 +11,7 @@ rm -rf node_modules/typedoc/node_modules/typescript # !\ can cause bugs, be vigilent with it # generate docs their own version folder -npx typedoc --out "docs/${DOCS_VERSION}" src --theme node_modules/eledoc/bin/default/ && +npx typedoc --out "docs/${DOCS_VERSION}" sources --theme node_modules/eledoc/bin/default/ && # & for github to display them correctly touch "docs/${DOCS_VERSION}/.nojekyll" diff --git a/scripts/build/types.sh b/scripts/build/types.sh index bd58239d2..702fb9ac8 100644 --- a/scripts/build/types.sh +++ b/scripts/build/types.sh @@ -2,11 +2,4 @@ mkdir -p out -npx tsc sources/ts-toolbelt.ts -d --emitDeclarationOnly --module amd --outFile out/index.d.ts - -# echo " -# declare module 'ts-toolbelt' { -# import main = require('ts-toolbelt'); -# export = main; -# } -# " >> out/index.d.ts +npx tsc sources/index.ts -d --emitDeclarationOnly --outDir out diff --git a/scripts/ci/branch/ad.sh b/scripts/ci/branch/ad.sh index 2c6e675cc..87a0319df 100644 --- a/scripts/ci/branch/ad.sh +++ b/scripts/ci/branch/ad.sh @@ -1,3 +1,3 @@ #!/bin/bash -bash ./scr/tools/check-deploy.sh +bash ./scripts/tools/check-deploy.sh diff --git a/scripts/ci/master/ad.sh b/scripts/ci/master/ad.sh index fb80f2944..7904150d3 100644 --- a/scripts/ci/master/ad.sh +++ b/scripts/ci/master/ad.sh @@ -1,3 +1,3 @@ #!/bin/bash -bash ./scr/tools/check-deploy.sh \ No newline at end of file +bash ./scripts/tools/check-deploy.sh \ No newline at end of file diff --git a/scripts/ci/test/ad.sh b/scripts/ci/test/ad.sh index ab192a659..670ecbf0a 100644 --- a/scripts/ci/test/ad.sh +++ b/scripts/ci/test/ad.sh @@ -4,7 +4,7 @@ # git clone --depth=1 https://github.com/DefinitelyTyped/DefinitelyTyped.git dt && -# npx ts-node scr/tools/dt-update-version.ts && +# npx ts-node scripts/tools/dt-update-version.ts && # cd dt && diff --git a/scripts/ci/test/bd.sh b/scripts/ci/test/bd.sh index df555d993..17142be68 100644 --- a/scripts/ci/test/bd.sh +++ b/scripts/ci/test/bd.sh @@ -2,6 +2,6 @@ npm run build:clean -npx ts-node scr/tools/package-test-version.ts && +npx ts-node scripts/tools/package-test-version.ts && npm run build:code && npm run build:types \ No newline at end of file diff --git a/sources/Any/At.ts b/sources/Any/At.ts new file mode 100644 index 000000000..c6e245f59 --- /dev/null +++ b/sources/Any/At.ts @@ -0,0 +1,34 @@ +import {Key} from './Key' +import {List} from '../List/List' + +/** + * Get in `O` the type of a field of key `K` + * @param O to extract from + * @param K to extract at + * @returns [[Any]] + * @example + * ```ts + * import {O} from 'ts-toolbelt' + * + * type User = { + * info: { + * name: string + * age: number + * payment: {} + * } + * id: number + * } + * + * type test0 = O.At // number + * ``` + */ +export type At = + A extends List + ? number extends A['length'] + ? K extends number | `${number}` + ? A[never] | undefined + : undefined + : K extends keyof A ? A[K] : undefined + : unknown extends A ? unknown : + K extends keyof A ? A[K] : + undefined; diff --git a/sources/Any/Compute.ts b/sources/Any/Compute.ts index c98448b71..b1f0e059a 100644 --- a/sources/Any/Compute.ts +++ b/sources/Any/Compute.ts @@ -1,5 +1,8 @@ import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' +import {Has} from '../Union/Has' +import {If} from './If' +import {Key} from './Key' /** * @hidden @@ -7,31 +10,38 @@ import {BuiltInObject} from '../Misc/BuiltInObject' export type ComputeRaw = A extends Function ? A - : { - [K in keyof A]: A[K] - } & {} + : {[K in keyof A]: A[K]} & unknown /** * @hidden */ -export type ComputeFlat = - A extends BuiltInObject - ? A - : { - [K in keyof A]: A[K] - } & {} +type ComputeFlat = + A extends BuiltIn ? A : + A extends Array + ? A extends Array> + ? Array<{[K in keyof A[number]]: A[number][K]} & unknown> + : A + : A extends ReadonlyArray + ? A extends ReadonlyArray> + ? ReadonlyArray<{[K in keyof A[number]]: A[number][K]} & unknown> + : A + : {[K in keyof A]: A[K]} & unknown; /** * @hidden */ -export type ComputeDeep = - A extends object - ? A extends BuiltInObject | Seen - ? A - : { - [K in keyof A]: ComputeDeep - } & {} - : A +type ComputeDeep = + A extends BuiltIn ? A : If, A, ( + A extends Array + ? A extends Array> + ? Array<{[K in keyof A[number]]: ComputeDeep} & unknown> + : A + : A extends ReadonlyArray + ? A extends ReadonlyArray> + ? ReadonlyArray<{[K in keyof A[number]]: ComputeDeep} & unknown> + : A + : {[K in keyof A]: ComputeDeep} & unknown + )>; /** * Force TS to load a type that has not been computed (to resolve composed diff --git a/sources/Any/If.ts b/sources/Any/If.ts new file mode 100644 index 000000000..6247cccb6 --- /dev/null +++ b/sources/Any/If.ts @@ -0,0 +1,6 @@ +import {Boolean} from '../Boolean/_Internal' + +export type If = + B extends 1 + ? Then + : Else diff --git a/sources/Any/Keys.ts b/sources/Any/Keys.ts new file mode 100644 index 000000000..71ab3722a --- /dev/null +++ b/sources/Any/Keys.ts @@ -0,0 +1,14 @@ +import {List} from '../List/List' + +/** + * Get the keys of `A` + * @param A + * @returns [[Key]] + * @example + * ```ts + * ``` + */ +export type Keys = + A extends List + ? Exclude | number + : keyof A diff --git a/sources/Any/KnownKeys.ts b/sources/Any/KnownKeys.ts new file mode 100644 index 000000000..60c5f195e --- /dev/null +++ b/sources/Any/KnownKeys.ts @@ -0,0 +1,18 @@ +import {Keys} from './Keys' + +/** + * Get the known keys of an [[Object]] + * @param O + * @returns [[Key]] + * @example + * ```ts + * ``` + */ +export type KnownKeys = { + [K in keyof O]: + string extends K ? never : + number extends K ? never : + K +} extends { + [K in keyof O]: infer U +} ? U & Keys : never; diff --git a/sources/Any/_api.ts b/sources/Any/_api.ts index 6c3fbc28c..1b6031864 100644 --- a/sources/Any/_api.ts +++ b/sources/Any/_api.ts @@ -1,12 +1,15 @@ /** @ignore *//** */ export {Await} from './Await' +export {At} from './At' export {Cast} from './Cast' export {Compute} from './Compute' export {Contains} from './Contains' export {Equals} from './Equals' export {Extends} from './Extends' export {Key} from './Key' +export {Keys} from './Keys' +export {KnownKeys} from './KnownKeys' export {Is} from './Is' export {Promise} from './Promise' export {Try} from './Try' diff --git a/sources/Community/IncludesDeep.ts b/sources/Community/IncludesDeep.ts index 0879455d9..d8a14642e 100644 --- a/sources/Community/IncludesDeep.ts +++ b/sources/Community/IncludesDeep.ts @@ -1,7 +1,6 @@ import {Match} from '../Any/_Internal' import {UnionOf} from '../Object/UnionOf' import {Next} from '../Iteration/Next' -import {Key} from '../Iteration/Key' import {Prev} from '../Iteration/Prev' import {Iteration} from '../Iteration/Iteration' import {IterationOf} from '../Iteration/IterationOf' diff --git a/sources/Function/AutoPath.ts b/sources/Function/AutoPath.ts new file mode 100644 index 000000000..548bf56f9 --- /dev/null +++ b/sources/Function/AutoPath.ts @@ -0,0 +1,96 @@ +import {Key} from '../Any/Key' +import {Head} from '../List/Head' +import {List} from '../List/List' +import {Pop} from '../List/Pop' +import {Tail} from '../List/Tail' +import {Path} from '../Object/Path' +import {UnionOf} from '../Object/UnionOf' +import {Select} from '../Union/Select' +import {Join} from '../String/Join' +import {Split} from '../String/Split' + +/** + * @ignore + */ +type Index = number | string; + +/** + * @ignore + */ +type KeyToIndex> = + number extends K ? Head : K & Index; + +/** + * @ignore + */ +type MetaPath = [], P extends List = []> = { + [K in keyof O]: + | MetaPath, [...P, KeyToIndex]> + | Join<[...P, KeyToIndex], D>; +}; + +/** + * @ignore + */ +type NextPath = + // the next paths after property `K` are on sub objects + // O[K] === K | {x: '${K}.x' | {y: '${K}.x.y' ...}} + // So we access O[K] then we only keep the next paths + // To do this, we can just exclude `string` out of it: + // O[K] === {x: '${K}.x' | {y: '${K}.x.y' ...}} + // To do this, we create a union of what we just got + // This will yield a union of paths and meta paths + // We exclude the next paths (meta) paths by excluding + // `object`. Then we are left with the direct next path + Select & {}>, string>; + +/** + * @ignore + */ +type ExecPath, Delimiter extends string> = + // We go in the `MetaPath` of `O` to get the prop at `SP` + // So we query what is going the `NextPath` at `O[...SP]` + NextPath, SP>>; + +/** + * @ignore + */ +type HintPath, Exec extends string, D extends string> = [Exec] extends [never] // if has not found paths + ? ExecPath, D> // display previous paths + : Exec | P; // display current + next + +/** + * @ignore + */ +type _AutoPath = Split> = + HintPath, D>; + +/** + * Auto-complete, validate, and query the string path of an object `O` + * @param O to work on + * @param P path of `O` + * @param D (?=`'.'`) delimiter for path + * + * ```ts + * declare function get( + * object: O, path: AutoPath + * ): Path> + * + * declare const user: User + * + * type User = { + * name: string + * friends: User[] + * } + * + * // works + * const friendName = get(user, 'friends.40.name') + * const friendFriendName = get(user, 'friends.40.friends.12.name') + * + * // errors + * const friendNames = get(user, 'friends.40.names') + * const friendFriendNames = get(user, 'friends.40.friends.12.names') + * ``` + */ +export type AutoPath = + _AutoPath; diff --git a/sources/Function/Compose/List/Async.ts b/sources/Function/Compose/List/Async.ts index c70f54df8..6d7f0eee0 100644 --- a/sources/Function/Compose/List/Async.ts +++ b/sources/Function/Compose/List/Async.ts @@ -2,6 +2,7 @@ import {Function} from '../../Function' import {Await} from '../../../Any/Await' +import {Curry} from '../../Curry' /** *@hidden @@ -11,7 +12,7 @@ export type ComposeListAsync = { R0, P extends any[], >(fns: [ - Function, + Function | Curry>, ]): Function>> < @@ -19,8 +20,8 @@ export type ComposeListAsync = { R1, P extends any[], >(fns: [ - Function<[Await], R1>, - Function, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -29,9 +30,9 @@ export type ComposeListAsync = { R2, P extends any[], >(fns: [ - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -41,10 +42,10 @@ export type ComposeListAsync = { R3, P extends any[], >(fns: [ - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -55,11 +56,11 @@ export type ComposeListAsync = { R4, P extends any[], >(fns: [ - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -71,12 +72,12 @@ export type ComposeListAsync = { R5, P extends any[], >(fns: [ - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -89,13 +90,13 @@ export type ComposeListAsync = { R6, P extends any[], >(fns: [ - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -109,14 +110,14 @@ export type ComposeListAsync = { R7, P extends any[], >(fns: [ - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -131,15 +132,15 @@ export type ComposeListAsync = { R8, P extends any[], >(fns: [ - Function<[Await], R8>, - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -155,15 +156,15 @@ export type ComposeListAsync = { R9, P extends any[], >(fns: [ - Function<[Await], R9>, - Function<[Await], R8>, - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R9> | Curry], R9>>, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> } diff --git a/sources/Function/Compose/List/Sync.ts b/sources/Function/Compose/List/Sync.ts index 56121cbc8..670f3a77f 100644 --- a/sources/Function/Compose/List/Sync.ts +++ b/sources/Function/Compose/List/Sync.ts @@ -1,5 +1,6 @@ /** @ignore *//** */ +import {Curry} from '../../Curry' import {Function} from '../../Function' /** @@ -10,7 +11,7 @@ export type ComposeListSync = { R0, P extends any[], >(fns: [ - Function, + Function | Curry>, ]): Function < @@ -18,8 +19,8 @@ export type ComposeListSync = { R1, P extends any[], >(fns: [ - Function<[R0], R1>, - Function, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -28,9 +29,9 @@ export type ComposeListSync = { R2, P extends any[], >(fns: [ - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -40,10 +41,10 @@ export type ComposeListSync = { R3, P extends any[], >(fns: [ - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -54,11 +55,11 @@ export type ComposeListSync = { R4, P extends any[], >(fns: [ - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -70,12 +71,12 @@ export type ComposeListSync = { R5, P extends any[], >(fns: [ - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -88,13 +89,13 @@ export type ComposeListSync = { R6, P extends any[], >(fns: [ - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -108,14 +109,14 @@ export type ComposeListSync = { R7, P extends any[], >(fns: [ - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -130,15 +131,15 @@ export type ComposeListSync = { R8, P extends any[], >(fns: [ - Function<[R7], R8>, - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R7], R8> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -154,15 +155,15 @@ export type ComposeListSync = { R9, P extends any[], >(fns: [ - Function<[R8], R9>, - Function<[R7], R8>, - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R8], R9> | Curry>, + Function<[R7], R8> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function } diff --git a/sources/Function/Compose/Multi/Async.ts b/sources/Function/Compose/Multi/Async.ts index d47b12799..2f6ad4cd4 100644 --- a/sources/Function/Compose/Multi/Async.ts +++ b/sources/Function/Compose/Multi/Async.ts @@ -2,6 +2,7 @@ import {Function} from '../../Function' import {Await} from '../../../Any/Await' +import {Curry} from '../../Curry' /** *@hidden @@ -11,7 +12,7 @@ export type ComposeMultiAsync = { R0, P extends any[], >(...fns: [ - Function, + Function | Curry>, ]): Function>> < @@ -19,8 +20,8 @@ export type ComposeMultiAsync = { R1, P extends any[], >(...fns: [ - Function<[Await], R1>, - Function, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -29,9 +30,9 @@ export type ComposeMultiAsync = { R2, P extends any[], >(...fns: [ - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -41,10 +42,10 @@ export type ComposeMultiAsync = { R3, P extends any[], >(...fns: [ - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -55,11 +56,11 @@ export type ComposeMultiAsync = { R4, P extends any[], >(...fns: [ - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -71,12 +72,12 @@ export type ComposeMultiAsync = { R5, P extends any[], >(...fns: [ - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -89,13 +90,13 @@ export type ComposeMultiAsync = { R6, P extends any[], >(...fns: [ - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -109,14 +110,14 @@ export type ComposeMultiAsync = { R7, P extends any[], >(...fns: [ - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -131,15 +132,15 @@ export type ComposeMultiAsync = { R8, P extends any[], >(...fns: [ - Function<[Await], R8>, - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> < @@ -155,15 +156,15 @@ export type ComposeMultiAsync = { R9, P extends any[], >(...fns: [ - Function<[Await], R9>, - Function<[Await], R8>, - Function<[Await], R7>, - Function<[Await], R6>, - Function<[Await], R5>, - Function<[Await], R4>, - Function<[Await], R3>, - Function<[Await], R2>, - Function<[Await], R1>, - Function, + Function<[Await], R9> | Curry], R9>>, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R1> | Curry], R1>>, + Function | Curry>, ]): Function>> } diff --git a/sources/Function/Compose/Multi/Sync.ts b/sources/Function/Compose/Multi/Sync.ts index 67503d1a0..e1ed6f193 100644 --- a/sources/Function/Compose/Multi/Sync.ts +++ b/sources/Function/Compose/Multi/Sync.ts @@ -1,6 +1,7 @@ /** @ignore *//** */ +import {Curry} from '../../Curry' import {Function} from '../../Function' /** @@ -11,7 +12,7 @@ export type ComposeMultiSync = { R0, P extends any[], >(...fns: [ - Function, + Function | Curry>, ]): Function < @@ -19,8 +20,8 @@ export type ComposeMultiSync = { R1, P extends any[], >(...fns: [ - Function<[R0], R1>, - Function, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -29,9 +30,9 @@ export type ComposeMultiSync = { R2, P extends any[], >(...fns: [ - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -41,10 +42,10 @@ export type ComposeMultiSync = { R3, P extends any[], >(...fns: [ - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -55,11 +56,11 @@ export type ComposeMultiSync = { R4, P extends any[], >(...fns: [ - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -71,12 +72,12 @@ export type ComposeMultiSync = { R5, P extends any[], >(...fns: [ - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -89,13 +90,13 @@ export type ComposeMultiSync = { R6, P extends any[], >(...fns: [ - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -109,14 +110,14 @@ export type ComposeMultiSync = { R7, P extends any[], >(...fns: [ - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -131,15 +132,15 @@ export type ComposeMultiSync = { R8, P extends any[], >(...fns: [ - Function<[R7], R8>, - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R7], R8> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function < @@ -155,15 +156,15 @@ export type ComposeMultiSync = { R9, P extends any[], >(...fns: [ - Function<[R8], R9>, - Function<[R7], R8>, - Function<[R6], R7>, - Function<[R5], R6>, - Function<[R4], R5>, - Function<[R3], R4>, - Function<[R2], R3>, - Function<[R1], R2>, - Function<[R0], R1>, - Function, + Function<[R8], R9> | Curry>, + Function<[R7], R8> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R0], R1> | Curry>, + Function | Curry>, ]): Function } diff --git a/sources/Function/Curry.ts b/sources/Function/Curry.ts index 28d6baa33..1afcf4061 100644 --- a/sources/Function/Curry.ts +++ b/sources/Function/Curry.ts @@ -25,7 +25,7 @@ type _SplitParams

? 2 : P extends [] ? 1 @@ -49,7 +49,7 @@ type _JoinParams = { 1: L 2: PSplit[number][] }[ - number extends PSplit['length'] + number extends Length ? 2 : PSplit extends [] ? 1 diff --git a/sources/Function/Exact.ts b/sources/Function/Exact.ts new file mode 100644 index 000000000..ab1df20c0 --- /dev/null +++ b/sources/Function/Exact.ts @@ -0,0 +1,22 @@ +import {Narrowable} from './_Internal' + +/** + * Force `A` to comply with `W`. `A` must be a shape of `W`. In other words, `A` + * must extend `W` and have the same properties - no more, no less. + * @param A + * @param W + */ +type Exact = +W extends unknown ? + A extends W + ? A extends Narrowable + ? A + : { + [K in keyof A]: K extends keyof W + ? Exact + : never + } + : W +: never; + +export {Exact} diff --git a/sources/Function/Function.ts b/sources/Function/Function.ts index a06fe8c4b..4cb2acc59 100644 --- a/sources/Function/Function.ts +++ b/sources/Function/Function.ts @@ -13,6 +13,5 @@ import {List} from '../List/List' * /// (args_0: string, args_1: number) => boolean * ``` */ -export interface Function

{ - (...args: P): R -} +export type Function

= + (...args: P) => R diff --git a/sources/Function/Length.ts b/sources/Function/Length.ts index 01a32ce5d..6b21be87d 100644 --- a/sources/Function/Length.ts +++ b/sources/Function/Length.ts @@ -1,12 +1,10 @@ import {Function} from './Function' import {Parameters} from './Parameters' -import {Formats} from '../Iteration/_Internal' import {Length as LLength} from '../List/Length' /** * Extract arguments' length from a [[Function]] * @param F to extract from - * @param fmt (?=`'n'`) output * @returns [[String]] or `number` * @example * ```ts @@ -21,5 +19,5 @@ import {Length as LLength} from '../List/Length' * type test2 = F.LengthOf<(...a: any[]) => any> // number * ``` */ -export type Length = - LLength, fmt> +export type Length = + LLength> diff --git a/sources/Function/Narrow.ts b/sources/Function/Narrow.ts new file mode 100644 index 000000000..11846f12f --- /dev/null +++ b/sources/Function/Narrow.ts @@ -0,0 +1,35 @@ +import {Try} from '../Any/Try' +import {Narrowable} from './_Internal' + +/** + * @hidden + */ +type NarrowRaw = +| (A extends [] ? [] : never) +| (A extends Narrowable ? A : never) +| ({[K in keyof A]: A[K] extends Function + ? A[K] + : NarrowRaw}); + +/** + * Prevent type widening on generic function parameters + * @param A to narrow + * @returns `A` + * @example + * ```ts + * import {F} from 'ts-toolbelt' + * + * declare function foo(x: F.Narrow): A; + * declare function bar(x: F.Narrow): A; + * + * const test0 = foo(['e', 2, true, {f: ['g', ['h']]}]) + * // `A` inferred : ['e', 2, true, {f: ['g']}] + * + * const test1 = bar({a: 1, b: 'c', d: ['e', 2, true, {f: ['g']}]}) + * // `A` inferred : {a: 1, b: 'c', d: ['e', 2, true, {f: ['g']}]} + * ``` + */ +type Narrow = + Try> + +export {Narrow} diff --git a/sources/Function/Pipe/List/Async.ts b/sources/Function/Pipe/List/Async.ts index 8a7b0c3cf..98b3090d6 100644 --- a/sources/Function/Pipe/List/Async.ts +++ b/sources/Function/Pipe/List/Async.ts @@ -1,6 +1,7 @@ /** @ignore *//** */ import {Function} from '../../Function' import {Await} from '../../../Any/Await' +import {Curry} from '../../Curry' /** *@hidden @@ -10,7 +11,7 @@ export type PipeListAsync = { R0, P extends any[], >(fns: [ - Function, + Function | Curry>, ]): Function>> < @@ -18,8 +19,8 @@ export type PipeListAsync = { R1, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, ]): Function>> < @@ -28,9 +29,9 @@ export type PipeListAsync = { R2, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, ]): Function>> < @@ -40,10 +41,10 @@ export type PipeListAsync = { R3, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, ]): Function>> < @@ -54,11 +55,11 @@ export type PipeListAsync = { R4, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, ]): Function>> < @@ -70,12 +71,12 @@ export type PipeListAsync = { R5, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, ]): Function>> < @@ -88,13 +89,13 @@ export type PipeListAsync = { R6, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, ]): Function>> < @@ -108,14 +109,14 @@ export type PipeListAsync = { R7, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, ]): Function>> < @@ -130,15 +131,15 @@ export type PipeListAsync = { R8, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, - Function<[Await], R8>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R8> | Curry], R8>>, ]): Function>> < @@ -154,15 +155,15 @@ export type PipeListAsync = { R9, P extends any[], >(fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, - Function<[Await], R8>, - Function<[Await], R9>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R9> | Curry], R9>>, ]): Function>> } diff --git a/sources/Function/Pipe/List/Sync.ts b/sources/Function/Pipe/List/Sync.ts index 06d8a06e2..9582cea63 100644 --- a/sources/Function/Pipe/List/Sync.ts +++ b/sources/Function/Pipe/List/Sync.ts @@ -1,5 +1,6 @@ /** @ignore *//** */ +import {Curry} from '../../Curry' import {Function} from '../../Function' /** @@ -10,7 +11,7 @@ export type PipeListSync = { R0, P extends any[], >(fns: [ - Function, + Function | Curry>, ]): Function < @@ -18,8 +19,8 @@ export type PipeListSync = { R1, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, + Function | Curry>, + Function<[R0], R1> | Curry>, ]): Function < @@ -28,9 +29,9 @@ export type PipeListSync = { R2, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, ]): Function < @@ -40,10 +41,10 @@ export type PipeListSync = { R3, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, ]): Function < @@ -54,11 +55,11 @@ export type PipeListSync = { R4, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, ]): Function < @@ -70,12 +71,12 @@ export type PipeListSync = { R5, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, ]): Function < @@ -88,13 +89,13 @@ export type PipeListSync = { R6, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, ]): Function < @@ -108,14 +109,14 @@ export type PipeListSync = { R7, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, ]): Function < @@ -130,15 +131,15 @@ export type PipeListSync = { R8, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, - Function<[R7], R8>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R7], R8> | Curry>, ]): Function < @@ -154,15 +155,15 @@ export type PipeListSync = { R9, P extends any[], >(fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, - Function<[R7], R8>, - Function<[R8], R9>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R7], R8> | Curry>, + Function<[R8], R9> | Curry>, ]): Function } diff --git a/sources/Function/Pipe/Multi/Async.ts b/sources/Function/Pipe/Multi/Async.ts index b4fc565b9..9799d5a66 100644 --- a/sources/Function/Pipe/Multi/Async.ts +++ b/sources/Function/Pipe/Multi/Async.ts @@ -2,6 +2,7 @@ import {Function} from '../../Function' import {Await} from '../../../Any/Await' +import {Curry} from '../../Curry' /** *@hidden @@ -11,7 +12,7 @@ export type PipeMultiAsync = { R0, P extends any[], >(...fns: [ - Function, + Function | Curry>, ]): Function>> < @@ -19,8 +20,8 @@ export type PipeMultiAsync = { R1, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, ]): Function>> < @@ -29,9 +30,9 @@ export type PipeMultiAsync = { R2, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, ]): Function>> < @@ -41,10 +42,10 @@ export type PipeMultiAsync = { R3, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, ]): Function>> < @@ -55,11 +56,11 @@ export type PipeMultiAsync = { R4, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, ]): Function>> < @@ -71,12 +72,12 @@ export type PipeMultiAsync = { R5, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, ]): Function>> < @@ -89,13 +90,13 @@ export type PipeMultiAsync = { R6, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, ]): Function>> < @@ -109,14 +110,14 @@ export type PipeMultiAsync = { R7, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, ]): Function>> < @@ -131,15 +132,15 @@ export type PipeMultiAsync = { R8, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, - Function<[Await], R8>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R8> | Curry], R8>>, ]): Function>> < @@ -155,15 +156,15 @@ export type PipeMultiAsync = { R9, P extends any[], >(...fns: [ - Function, - Function<[Await], R1>, - Function<[Await], R2>, - Function<[Await], R3>, - Function<[Await], R4>, - Function<[Await], R5>, - Function<[Await], R6>, - Function<[Await], R7>, - Function<[Await], R8>, - Function<[Await], R9>, + Function | Curry>, + Function<[Await], R1> | Curry], R1>>, + Function<[Await], R2> | Curry], R2>>, + Function<[Await], R3> | Curry], R3>>, + Function<[Await], R4> | Curry], R4>>, + Function<[Await], R5> | Curry], R5>>, + Function<[Await], R6> | Curry], R6>>, + Function<[Await], R7> | Curry], R7>>, + Function<[Await], R8> | Curry], R8>>, + Function<[Await], R9> | Curry], R9>>, ]): Function>> } diff --git a/sources/Function/Pipe/Multi/Sync.ts b/sources/Function/Pipe/Multi/Sync.ts index 81e49ab04..b67937e27 100644 --- a/sources/Function/Pipe/Multi/Sync.ts +++ b/sources/Function/Pipe/Multi/Sync.ts @@ -1,5 +1,6 @@ /** @ignore *//** */ +import {Curry} from '../../Curry' import {Function} from '../../Function' /** @@ -10,7 +11,7 @@ export type PipeMultiSync = { R0, P extends any[], >(...fns: [ - Function, + Function | Curry>, ]): Function < @@ -18,8 +19,8 @@ export type PipeMultiSync = { R1, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, + Function | Curry>, + Function<[R0], R1> | Curry>, ]): Function < @@ -28,9 +29,9 @@ export type PipeMultiSync = { R2, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, ]): Function < @@ -40,10 +41,10 @@ export type PipeMultiSync = { R3, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, ]): Function < @@ -54,11 +55,11 @@ export type PipeMultiSync = { R4, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, ]): Function < @@ -70,12 +71,12 @@ export type PipeMultiSync = { R5, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, ]): Function < @@ -88,13 +89,13 @@ export type PipeMultiSync = { R6, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, ]): Function < @@ -108,14 +109,14 @@ export type PipeMultiSync = { R7, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, ]): Function < @@ -130,15 +131,15 @@ export type PipeMultiSync = { R8, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, - Function<[R7], R8>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R7], R8> | Curry>, ]): Function < @@ -154,15 +155,15 @@ export type PipeMultiSync = { R9, P extends any[], >(...fns: [ - Function, - Function<[R0], R1>, - Function<[R1], R2>, - Function<[R2], R3>, - Function<[R3], R4>, - Function<[R4], R5>, - Function<[R5], R6>, - Function<[R6], R7>, - Function<[R7], R8>, - Function<[R8], R9>, + Function | Curry>, + Function<[R0], R1> | Curry>, + Function<[R1], R2> | Curry>, + Function<[R2], R3> | Curry>, + Function<[R3], R4> | Curry>, + Function<[R4], R5> | Curry>, + Function<[R5], R6> | Curry>, + Function<[R6], R7> | Curry>, + Function<[R7], R8> | Curry>, + Function<[R8], R9> | Curry>, ]): Function } diff --git a/sources/Object/PathValid.ts b/sources/Function/ValidPath.ts similarity index 77% rename from sources/Object/PathValid.ts rename to sources/Function/ValidPath.ts index dc3d30797..b93ff5d0c 100644 --- a/sources/Object/PathValid.ts +++ b/sources/Function/ValidPath.ts @@ -2,7 +2,7 @@ import {IterationOf} from '../Iteration/IterationOf' import {Iteration} from '../Iteration/Iteration' import {Next} from '../Iteration/Next' import {Pos} from '../Iteration/Pos' -import {At} from './At' +import {At} from '../Any/At' import {Cast} from '../Any/Cast' import {NonNullable} from '../Union/NonNullable' import {Update} from '../List/Update' @@ -27,16 +27,16 @@ type ValidatePath, I extends Iteration> = /** * @hidden */ -type __PathValid, I extends Iteration = IterationOf<0>> = { - 0: __PathValid]>>, ValidatePath, Next> +type __ValidPath, I extends Iteration = IterationOf<0>> = { + 0: __ValidPath]>>, ValidatePath, Next> 1: Path }[Extends, Length>] /** * @hidden */ -export type _PathValid> = - __PathValid extends infer X +export type _ValidPath> = + __ValidPath extends infer X ? Cast> : never @@ -50,11 +50,11 @@ export type _PathValid> = * import {A, L, O} from 'ts-toolbelt' * * // Get a property in an object `o` at any depth with `path` - * // `A.Cast>` makes sure `path` is valid + * // `A.Cast>` makes sure `path` is valid * const getAt = < * O extends object, * P extends L.List - * >(o: O, path: A.Cast>): O.Path => { + * >(o: O, path: A.Cast>): O.Path => { * let valueAt = o * * for (const p of path) @@ -68,9 +68,9 @@ export type _PathValid> = * const test2 = getAt({a: {b: {c: 1}}}, ['x'] as const) // error * ``` */ -export type PathValid> = +export type ValidPath> = O extends unknown ? Path extends unknown - ? _PathValid + ? _ValidPath : never : never diff --git a/sources/Function/_Internal.ts b/sources/Function/_Internal.ts index 780aee4e9..61f747ddb 100644 --- a/sources/Function/_Internal.ts +++ b/sources/Function/_Internal.ts @@ -12,3 +12,11 @@ export type Mode = 'sync' | 'async' */ export type Input = 'multi' | 'list' +/** + * Describes types that can be narrowed + */ +export type Narrowable = +| string +| number +| bigint +| boolean diff --git a/sources/Function/_api.ts b/sources/Function/_api.ts index 6d25f4238..c34e9a648 100644 --- a/sources/Function/_api.ts +++ b/sources/Function/_api.ts @@ -1,8 +1,11 @@ /** @ignore *//** */ +export {AutoPath} from './AutoPath' export {Compose} from './Compose' +export {Exact} from './Exact' export {Curry} from './Curry' export {Function} from './Function' +export {Narrow} from './Narrow' export {Length} from './Length' export {NoInfer} from './NoInfer' export {Parameters} from './Parameters' @@ -10,3 +13,4 @@ export {Pipe} from './Pipe' export {Promisify} from './Promisify' export {Return} from './Return' export {UnCurry} from './UnCurry' +export {ValidPath} from './ValidPath' diff --git a/sources/Iteration/IterationOf.ts b/sources/Iteration/IterationOf.ts index fb104a2ba..d07ba7525 100644 --- a/sources/Iteration/IterationOf.ts +++ b/sources/Iteration/IterationOf.ts @@ -4,7 +4,6 @@ import {IterationMap} from './Iteration' * Transform a number into an [[Iteration]] * (to use [[Prev]], [[Next]], & [[Pos]]) * @param N to transform - * @param IMap to operate with another set of numbers * @returns [[Iteration]] * @example * ```ts diff --git a/sources/Iteration/Next.ts b/sources/Iteration/Next.ts index caeab2926..35c8fc88b 100644 --- a/sources/Iteration/Next.ts +++ b/sources/Iteration/Next.ts @@ -3,7 +3,6 @@ import {Iteration, IterationMap} from './Iteration' /** * Move `I`'s position forward * @param I to move - * @param IMap to operate with another set of numbers * @returns [[Iteration]] * @example * ```ts diff --git a/sources/Iteration/Pos.ts b/sources/Iteration/Pos.ts index 4795c0f63..b41dbf0ef 100644 --- a/sources/Iteration/Pos.ts +++ b/sources/Iteration/Pos.ts @@ -3,7 +3,6 @@ import {Iteration} from './Iteration' /** * Get the position of `I` (**number**) * @param I to query - * @param IMap to operate with another set of numbers * @returns `number` * @example * ```ts diff --git a/sources/Iteration/Prev.ts b/sources/Iteration/Prev.ts index b09cbadfe..16f731671 100644 --- a/sources/Iteration/Prev.ts +++ b/sources/Iteration/Prev.ts @@ -3,7 +3,6 @@ import {Iteration, IterationMap} from './Iteration' /** * Move `I`'s position backwards * @param I to move - * @param IMap to operate with another set of numbers * @returns [[Iteration]] * @example * ```ts diff --git a/sources/Iteration/_Internal.ts b/sources/Iteration/_Internal.ts index e06650df4..b0b4610e6 100644 --- a/sources/Iteration/_Internal.ts +++ b/sources/Iteration/_Internal.ts @@ -1,10 +1,3 @@ -/** - * Describes compatible type formats - * `s`: `string` - * `n`: `number` - */ -export type Formats = 'n' | 's' - /** * Describes how to perform iterations */ diff --git a/sources/List/Assign.ts b/sources/List/Assign.ts index 41f51cd3a..27540877d 100644 --- a/sources/List/Assign.ts +++ b/sources/List/Assign.ts @@ -1,7 +1,7 @@ import {Assign as OAssign} from '../Object/Assign' import {List} from './List' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' /** @@ -10,8 +10,8 @@ import {Cast} from '../Any/Cast' * @param L to assign to * @param Ls to assign * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`undefined`) types of `O` to be replaced with ones of `O1` * @returns [[Object]] * @example * ```ts @@ -22,5 +22,5 @@ import {Cast} from '../Any/Cast' * type test2 = Assign<[0, 0, 0, 0, 0], [[0, 1], [0, 2, 0, 4?]]> // [0, 2, 0, 0 | 4, 0] * ``` */ -export type Assign, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type Assign, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = never> = Cast, List> diff --git a/sources/List/At.ts b/sources/List/At.ts deleted file mode 100644 index 8a6a00c57..000000000 --- a/sources/List/At.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {At as OAt} from '../Object/At' -import {Key} from '../Any/Key' -import {List} from './List' -import {Boolean} from '../Boolean/_Internal' - -/** - * Get in `L` the type of an entry of key `K`. - * @param L to extract from - * @param K to extract at - * @param strict (?=`1`) `0` to work with unions - * @returns [[Any]] - * @example - * ```ts - * import {L} from 'ts-toolbelt' - * - * type test0 = L.At<[1, 2, 3], 1> // 2 - * type test1 = L.At<[{a: string}, {b: string}], 0> // {a: string} - * ``` - */ -export type At = - OAt diff --git a/sources/List/AtLeast.ts b/sources/List/AtLeast.ts index 210aa554b..36b0abca2 100644 --- a/sources/List/AtLeast.ts +++ b/sources/List/AtLeast.ts @@ -3,7 +3,7 @@ import {AtLeast as OAtLeast} from '../Object/AtLeast' import {ObjectOf} from './ObjectOf' import {_ListOf} from '../Object/ListOf' import {List} from './List' -import {Keys} from './Keys' +import {Keys} from '../Any/Keys' /** * Make that at least one of the keys `K` are required in `L` at a time. @@ -24,7 +24,7 @@ import {Keys} from './Keys' * ``` */ export type AtLeast> = - OAtLeast, `${K}` | K> extends infer U + OAtLeast, `${K & number}` | K> extends infer U ? U extends unknown // we distribute over the union ? _ListOf // each union member to a list : never diff --git a/sources/List/CompulsoryKeys.ts b/sources/List/CompulsoryKeys.ts index e336c4a5b..f12e23a4e 100644 --- a/sources/List/CompulsoryKeys.ts +++ b/sources/List/CompulsoryKeys.ts @@ -1,4 +1,4 @@ -import {Compulsory as OCompulsory} from '../Object/Compulsory' +import {CompulsoryKeys as OCompulsoryKeys} from '../Object/CompulsoryKeys' import {ObjectOf} from './ObjectOf' import {List} from './List' @@ -16,4 +16,4 @@ import {List} from './List' * ``` */ export type CompulsoryKeys = - OCompulsory> + OCompulsoryKeys> diff --git a/sources/List/Either.ts b/sources/List/Either.ts index f62deeb8c..51b6a6207 100644 --- a/sources/List/Either.ts +++ b/sources/List/Either.ts @@ -17,7 +17,7 @@ import {Boolean} from '../Boolean/_Internal' * ``` */ export type Either = - OEither, `${K}` | K, strict> extends infer OE + OEither, `${K & number}` | K, strict> extends infer OE ? OE extends unknown ? _ListOf : never diff --git a/sources/List/Has.ts b/sources/List/Has.ts index 672bdbe6a..4a4dfea60 100644 --- a/sources/List/Has.ts +++ b/sources/List/Has.ts @@ -16,4 +16,4 @@ import {List} from './List' * ``` */ export type Has = - OHas, `${K}` | K, M, match> + OHas, `${K & number}` | K, M, match> diff --git a/sources/List/Keys.ts b/sources/List/Keys.ts deleted file mode 100644 index eedf90dc2..000000000 --- a/sources/List/Keys.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {Exclude} from '../Union/Exclude' -import {List} from './List' -import {Keys as UKeys} from '../Union/Keys' - -/** - * Get the keys of a [[List]] - * @param L - * @returns [[Key]] - * @example - * ```ts - * ``` - */ -export type Keys = - Exclude, keyof any[]> | number - // re-include `number`, it's a "own key" diff --git a/sources/List/LastIndex.ts b/sources/List/LastIndex.ts deleted file mode 100644 index 59fa81baf..000000000 --- a/sources/List/LastIndex.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {Length} from './Length' -import {Formats} from '../Iteration/_Internal' -import {Tail} from './Tail' -import {List} from './List' - -/** - * Get the last index of `L` - * @param L to get from - * @param fmt (?=`'n'`) output format - * @returns [[String]] or `number` - * @example - * ```ts - * ``` - */ -export type LastIndex = - Length, fmt> diff --git a/sources/List/LastKey.ts b/sources/List/LastKey.ts new file mode 100644 index 000000000..76be46ca9 --- /dev/null +++ b/sources/List/LastKey.ts @@ -0,0 +1,14 @@ +import {Length} from './Length' +import {Tail} from './Tail' +import {List} from './List' + +/** + * Get the last index of `L` + * @param L to get from + * @returns `number` + * @example + * ```ts + * ``` + */ +export type LastKey = + Length> diff --git a/sources/List/Length.ts b/sources/List/Length.ts index cc2ea6d53..83f3e3a22 100644 --- a/sources/List/Length.ts +++ b/sources/List/Length.ts @@ -1,16 +1,12 @@ -import {Formats} from '../Iteration/_Internal' import {List} from './List' /** * Get the length of `L` * @param L to get length - * @param fmt (?=`'n'`) output format * @returns [[String]] or `number` * @example * ```ts * ``` */ -export type Length = { - 's': `${L['length']}` - 'n': L['length'] -}[fmt] +export type Length = + L['length'] diff --git a/sources/List/Longest.ts b/sources/List/Longest.ts index 0806688c1..a2e86a779 100644 --- a/sources/List/Longest.ts +++ b/sources/List/Longest.ts @@ -1,6 +1,5 @@ -import {Exclude} from '../Union/Exclude' +import {Has} from '../Union/Has' import {List} from './List' -import {Keys} from './Keys' /** * Get the longest [[List]] of `L` & `L1` @@ -13,6 +12,6 @@ import {Keys} from './Keys' * ``` */ export type Longest = - [Exclude, Keys>] extends [never] - ? L - : L1 + L extends unknown ? L1 extends unknown ? + {0: L1, 1: L}[Has] + : never : never diff --git a/sources/List/Merge.ts b/sources/List/Merge.ts index 67d9e5197..1521cc7a3 100644 --- a/sources/List/Merge.ts +++ b/sources/List/Merge.ts @@ -1,7 +1,7 @@ import {Merge as OMerge} from '../Object/Merge' import {List} from './List' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' /** @@ -13,12 +13,12 @@ import {Cast} from '../Any/Cast' * @param L to complete * @param L1 to copy from * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`undefined`) types of `O` to be replaced with ones of `O1` * @returns [[List]] * @example * ```ts * ``` */ -export type Merge = +export type Merge = Cast, List> diff --git a/sources/List/MergeAll.ts b/sources/List/MergeAll.ts index cc1fb87e2..319a41225 100644 --- a/sources/List/MergeAll.ts +++ b/sources/List/MergeAll.ts @@ -1,7 +1,7 @@ import {MergeAll as OMergeAll} from '../Object/MergeAll' import {List} from '../List/List' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' /** @@ -10,12 +10,12 @@ import {Cast} from '../Any/Cast' * @param L to start with * @param Ls to merge * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`undefined`) types of `O` to be replaced with ones of `O1` * @returns [[List]] * @example * ```ts * ``` */ -export type MergeAll, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type MergeAll, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = undefined> = Cast, List> diff --git a/sources/List/Modify.ts b/sources/List/Modify.ts index fa386522f..f1b6413c4 100644 --- a/sources/List/Modify.ts +++ b/sources/List/Modify.ts @@ -1,8 +1,8 @@ -import {At} from './At' import {Replace} from '../Union/Replace' import {x} from '../Any/x' import {List} from './List' import {Cast} from '../Any/Cast' +import {At} from '../Any/At' /** * Modify `L` with `LMod` & the [[x]] placeholder diff --git a/sources/List/NonNullable.ts b/sources/List/NonNullable.ts index f8c45bbc0..31dc8bee3 100644 --- a/sources/List/NonNullable.ts +++ b/sources/List/NonNullable.ts @@ -2,7 +2,7 @@ import {_Pick} from '../Object/Pick' import {Key} from './_Internal' import {NonNullable as UNonNullable} from '../Union/NonNullable' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' import {List} from './List' @@ -19,7 +19,7 @@ export type NonNullableFlat = { * @hidden */ type _NonNullableDeep = { - [K in keyof O]: O[K] extends BuiltInObject + [K in keyof O]: O[K] extends BuiltIn ? O[K] : NonNullableDeep } @@ -49,4 +49,4 @@ export type NonNullablePart = - Cast, List> + Cast, List> diff --git a/sources/List/Nullable.ts b/sources/List/Nullable.ts index 17c9b6274..5b866f309 100644 --- a/sources/List/Nullable.ts +++ b/sources/List/Nullable.ts @@ -1,5 +1,4 @@ import {Key} from './_Internal' -import {Depth} from '../Object/_Internal' import {List} from './List' import {Update} from '../Object/Update' import {x} from '../Any/x' @@ -15,5 +14,5 @@ import {Cast} from '../Any/Cast' * ```ts * ``` */ -export type Nullable = - Cast, List> +export type Nullable = + Cast, List> diff --git a/sources/List/ObjectOf.ts b/sources/List/ObjectOf.ts index 65d32b201..1fbb46850 100644 --- a/sources/List/ObjectOf.ts +++ b/sources/List/ObjectOf.ts @@ -1,5 +1,6 @@ import {_Omit} from '../Object/Omit' import {_Pick} from '../Object/Pick' +import {Length} from './Length' import {List} from './List' /** @@ -10,9 +11,9 @@ import {List} from './List' * ```ts * ``` */ -export type ObjectOf = - O extends List - ? number extends O['length'] // detect arrays - ? _Pick // preserves arrays - : _Omit // transforms tuples - : O +export type ObjectOf = + O extends unknown + ? number extends Length // detect arrays + ? _Pick // preserves arrays + : _Omit // transforms tuples + : never diff --git a/sources/List/Omit.ts b/sources/List/Omit.ts index 268c9dcc1..e8cb5afa0 100644 --- a/sources/List/Omit.ts +++ b/sources/List/Omit.ts @@ -8,7 +8,7 @@ import {ObjectOf} from './ObjectOf' * @hidden */ export type _Omit = - _ListOf<_OOmit, `${K}` | K>> + _ListOf<_OOmit, `${K & number}` | K>> /** * Remove out of `L` the entries of key `K` diff --git a/sources/List/Patch.ts b/sources/List/Patch.ts index f2accd28f..a3036ac5f 100644 --- a/sources/List/Patch.ts +++ b/sources/List/Patch.ts @@ -1,7 +1,7 @@ import {Patch as OPatch} from '../Object/Patch' import {List} from './List' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' /** @@ -13,13 +13,12 @@ import {Cast} from '../Any/Cast' * @param L to complete * @param L1 to copy from * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param style (?=`1`) 0 = lodash, 1 = ramda - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`never`) types of `O` to be replaced with ones of `O1` * @returns [[List]] * @example * ```ts * ``` */ -export type Patch = +export type Patch = Cast, List> diff --git a/sources/List/PatchAll.ts b/sources/List/PatchAll.ts index 81397e6bb..e17124be7 100644 --- a/sources/List/PatchAll.ts +++ b/sources/List/PatchAll.ts @@ -1,7 +1,7 @@ import {PatchAll as OPatchAll} from '../Object/PatchAll' import {List} from '../List/List' import {Depth} from '../Object/_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Cast} from '../Any/Cast' /** @@ -10,12 +10,12 @@ import {Cast} from '../Any/Cast' * @param O to start with * @param Os to patch * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`never`) types of `O` to be replaced with ones of `O1` * @returns [[List]] * @example * ```ts * ``` */ -export type PatchAll, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type PatchAll, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = never> = Cast, List> diff --git a/sources/List/PathValid.ts b/sources/List/PathValid.ts deleted file mode 100644 index 459c6eab9..000000000 --- a/sources/List/PathValid.ts +++ /dev/null @@ -1,33 +0,0 @@ -import {PathValid as OPathValid} from '../Object/PathValid' -import {Key} from '../Any/Key' -import {List} from './List' - -/** - * Replaces invalid parts of a path with `never` - * @param L to be inspected - * @param Path to be validated - * @returns [[Index]][] - * @example - * ```ts - * import {A, L, O} from 'ts-toolbelt' - * - * // Get a property in an array `t` at any depth with `path` - * // `A.Cast>` makes sure `path` is valid - * const getAt = < - * L extends L.List, - * P extends L.List - * >(t: L, path: A.Cast>): L.Path => { - * let valueAt = t - * - * for (const p of path) - * valueAt = valueAt[p] - * - * return valueAt as any - * } - * - * const test0 = getAt([[[1]]] as const, [0, 0] as const) // [1] - * const test1 = getAt([[[1]]] as const, [1] as const) // error - * ``` - */ -export type PathValid> = - OPathValid diff --git a/sources/List/Pick.ts b/sources/List/Pick.ts index c637a1fe4..d315dd0a2 100644 --- a/sources/List/Pick.ts +++ b/sources/List/Pick.ts @@ -8,7 +8,7 @@ import {List} from './List' * @hidden */ export type _Pick = - _ListOf<_OPick, `${K}` | K>> + _ListOf<_OPick, `${K & number}` | K>> /** * Extract out of `L` the entries of key `K` diff --git a/sources/List/Repeat.ts b/sources/List/Repeat.ts index 9c0b4c662..a1745185d 100644 --- a/sources/List/Repeat.ts +++ b/sources/List/Repeat.ts @@ -3,7 +3,6 @@ import {Prepend} from './Prepend' import {IterationOf} from '../Iteration/IterationOf' import {Iteration} from '../Iteration/Iteration' import {Cast} from '../Any/Cast' -import {Key} from '../Iteration/Key' import {List} from './List' import {Extends} from '../Any/Extends' import {Pos} from '../Iteration/Pos' diff --git a/sources/List/Shortest.ts b/sources/List/Shortest.ts index ee979388b..d135ce070 100644 --- a/sources/List/Shortest.ts +++ b/sources/List/Shortest.ts @@ -1,6 +1,5 @@ -import {Exclude} from '../Union/Exclude' +import {Has} from '../Union/Has' import {List} from './List' -import {Keys} from './Keys' /** * Get the shortest [[List]] of `L` & `L1` @@ -13,6 +12,6 @@ import {Keys} from './Keys' * ``` */ export type Shortest = - [Exclude, Keys>] extends [never] - ? L - : L1 + L extends unknown ? L1 extends unknown ? + {0: L1, 1: L}[Has] + : never : never diff --git a/sources/List/Undefinable.ts b/sources/List/Undefinable.ts index b66f77408..fd7f69800 100644 --- a/sources/List/Undefinable.ts +++ b/sources/List/Undefinable.ts @@ -1,5 +1,4 @@ import {Key} from './_Internal' -import {Depth} from '../Object/_Internal' import {List} from './List' import {Update} from '../Object/Update' import {x} from '../Any/x' @@ -9,11 +8,10 @@ import {Cast} from '../Any/Cast' * Make some entries of `L` not `undefined` (deeply or not) * @param L to make non nullable * @param K (?=`Key`) to choose fields - * @param depth (?=`'flat'`) 'deep' to do it deeply * @returns [[List]] * @example * ```ts * ``` */ -export type Undefinable = - Cast, List> +export type Undefinable = + Cast, List> diff --git a/sources/List/Unionize.ts b/sources/List/Unionize.ts index 3847bb7e1..86a2fe9c0 100644 --- a/sources/List/Unionize.ts +++ b/sources/List/Unionize.ts @@ -1,7 +1,6 @@ import {Key} from '../Any/Key' import {List} from './List' -import {At} from '../Object/At' -import {Cast} from '../Any/Cast' +import {At} from '../Any/At' /** * Make the fields of `L` union the ones of `L1` @@ -13,8 +12,8 @@ import {Cast} from '../Any/Cast' * ```ts * ``` */ -export type Unionize = Cast<{ +export type Unionize = { [P in keyof L]: P extends K ? L[P] | At : L[P] -}, List> +} diff --git a/sources/List/Update.ts b/sources/List/Update.ts index 06b8275aa..70f555071 100644 --- a/sources/List/Update.ts +++ b/sources/List/Update.ts @@ -1,7 +1,6 @@ import {Key} from './_Internal' import {List} from './List' import {Update as OUpdate} from '../Object/Update' -import {Depth} from '../Object/_Internal' import {Cast} from '../Any/Cast' /** @@ -15,5 +14,5 @@ import {Cast} from '../Any/Cast' * ```ts * ``` */ -export type Update = - Cast, List> +export type Update = + Cast, List> diff --git a/sources/List/_Internal.ts b/sources/List/_Internal.ts index 6a4969379..45dea20c1 100644 --- a/sources/List/_Internal.ts +++ b/sources/List/_Internal.ts @@ -9,4 +9,4 @@ import {List} from './List' export type Naked = Overwrite, L> -export type Key = string | number +export type Key = string | number | symbol diff --git a/sources/List/_api.ts b/sources/List/_api.ts index a228d38be..db7334a89 100644 --- a/sources/List/_api.ts +++ b/sources/List/_api.ts @@ -2,7 +2,6 @@ export {Append} from './Append' export {Assign} from './Assign' -export {At} from './At' export {AtLeast} from './AtLeast' export {Compulsory} from './Compulsory' export {CompulsoryKeys} from './CompulsoryKeys' @@ -23,10 +22,9 @@ export {Head} from './Head' export {Includes} from './Includes' export {Intersect} from './Intersect' export {IntersectKeys} from './IntersectKeys' -export {Keys} from './Keys' export {KeySet} from './KeySet' export {Last} from './Last' -export {LastIndex} from './LastIndex' +export {LastKey} from './LastKey' export {Length} from './Length' export {List} from './List' export {Longest} from './Longest' @@ -47,7 +45,6 @@ export {Patch} from './Patch' export {PatchAll} from './PatchAll' export {Path} from './Path' export {Paths} from './Paths' -export {PathValid} from './PathValid' export {Pick} from './Pick' export {Pop} from './Pop' export {Prepend} from './Prepend' diff --git a/sources/Misc/BuiltInObject.ts b/sources/Misc/BuiltIn.ts similarity index 97% rename from sources/Misc/BuiltInObject.ts rename to sources/Misc/BuiltIn.ts index 982004691..f27e25254 100644 --- a/sources/Misc/BuiltInObject.ts +++ b/sources/Misc/BuiltIn.ts @@ -73,10 +73,10 @@ */ // type WebAssembly = never -export type BuiltInObject = +export type BuiltIn = + | Function | Error | Date - | RegExp - | Function | {readonly [Symbol.toStringTag]: string} + | RegExp | Generator diff --git a/sources/Misc/_api.ts b/sources/Misc/_api.ts index 8f1fc46ea..18be70504 100644 --- a/sources/Misc/_api.ts +++ b/sources/Misc/_api.ts @@ -3,5 +3,5 @@ import * as JSON from './JSON/_api' export {JSON} -export {BuiltInObject} from './BuiltInObject' +export {BuiltIn} from './BuiltIn' export {Primitive} from './Primitive' diff --git a/sources/Number/Absolute.ts b/sources/Number/Absolute.ts index 08caa64d7..080662bc7 100644 --- a/sources/Number/Absolute.ts +++ b/sources/Number/Absolute.ts @@ -14,8 +14,6 @@ export type _Absolute = { /** * Get the absolute value of a [[Number]] * @param N to absolute - * @param fmt (?=`'s'`) output format - * @param IMap to operate with another set of numbers * @returns `string | number | boolean` * @example * ```ts diff --git a/sources/Number/Add.ts b/sources/Number/Add.ts index bd41ff58a..149856865 100644 --- a/sources/Number/Add.ts +++ b/sources/Number/Add.ts @@ -60,18 +60,10 @@ export type _Add = { 1: AddNegative }[_IsNegative] -/** - * @hidden - */ -export type __Add = - _Add, IterationOf> - /** * Add a [[Number]] to another one * @param N1 Left-hand side * @param N2 Right-hand side - * @param fmt (?=`'s'`) output format - * @param IMap to operate with another set of numbers * @returns `string | number | boolean` * @example * ```ts @@ -88,6 +80,6 @@ export type __Add = export type Add = N1 extends unknown ? N2 extends unknown - ? __Add[0] - : never + ? _Add, IterationOf>[0] + : never : never diff --git a/sources/Number/Greater.ts b/sources/Number/Greater.ts index bdc5ff629..c6f852cdd 100644 --- a/sources/Number/Greater.ts +++ b/sources/Number/Greater.ts @@ -13,7 +13,6 @@ export type _Greater = * Check if a [[Number]] is bigger than another one * @param N1 to compare * @param N2 to compare to - * @param IMap to operate with another set of numbers * @returns [[Boolean]] * @example * ```ts diff --git a/sources/Number/GreaterEq.ts b/sources/Number/GreaterEq.ts index a2ae67bcd..f2c0daf68 100644 --- a/sources/Number/GreaterEq.ts +++ b/sources/Number/GreaterEq.ts @@ -14,7 +14,6 @@ export type _GreaterEq = * Check if a [[Number]] is greater or equal to another one * @param N1 to compare * @param N2 to compare to - * @param IMap to operate with another set of numbers * @returns [[Boolean]] * @example * ```ts diff --git a/sources/Number/IsZero.ts b/sources/Number/IsZero.ts index 4a5a9d25e..cc873a66e 100644 --- a/sources/Number/IsZero.ts +++ b/sources/Number/IsZero.ts @@ -13,7 +13,6 @@ export type _IsZero = { /** * Check whether a [[Number]] is null or not * @param N to check - * @param IMap to operate with another set of numbers * @returns [[Boolean]] * @example * ```ts diff --git a/sources/Number/Lower.ts b/sources/Number/Lower.ts index 573da3d6f..0b028907c 100644 --- a/sources/Number/Lower.ts +++ b/sources/Number/Lower.ts @@ -12,7 +12,6 @@ export type _Lower = * Check if a [[Number]] is lower than another one * @param N1 to compare * @param N2 to compare to - * @param IMap to operate with another set of numbers * @returns [[Boolean]] * @example * ```ts diff --git a/sources/Number/LowerEq.ts b/sources/Number/LowerEq.ts index cae335007..938ddf504 100644 --- a/sources/Number/LowerEq.ts +++ b/sources/Number/LowerEq.ts @@ -4,7 +4,6 @@ import {GreaterEq} from './GreaterEq' * Check if a [[Number]] is lower or equal to another one * @param N1 to compare * @param N2 to compare to - * @param IMap to operate with another set of numbers * @returns [[Boolean]] * @example * ```ts diff --git a/sources/Number/Negate.ts b/sources/Number/Negate.ts index 30e68fa1d..163c91ec3 100644 --- a/sources/Number/Negate.ts +++ b/sources/Number/Negate.ts @@ -11,8 +11,6 @@ export type _Negate = /** * Negate a [[Number]] * @param N to negate - * @param fmt (?=`'s'`) output format - * @param IMap to operate with another set of numbers * @returns `string | number | boolean` * @example * ```ts diff --git a/sources/Number/Range.ts b/sources/Number/Range.ts index 1bf03069c..5811dd3e2 100644 --- a/sources/Number/Range.ts +++ b/sources/Number/Range.ts @@ -46,8 +46,6 @@ export type _Range = * @param From to start with * @param To to end with * @param way (?=`'->'`) to reverse it - * @param fmt (?=`'s'`) output format - * @param IMap to operate with another set of numbers * @returns `string[] | number[] | boolean[]` * @example * ```ts diff --git a/sources/Number/Sub.ts b/sources/Number/Sub.ts index 8bed0ddc8..0d81506c0 100644 --- a/sources/Number/Sub.ts +++ b/sources/Number/Sub.ts @@ -60,18 +60,10 @@ export type _Sub = { 1: SubNegative }[_IsNegative] -/** - * @hidden - */ -export type __Sub = - _Sub, IterationOf> - /** * Subtract a [[Number]] from another one * @param N1 Left-hand side * @param N2 Right-hand side - * @param fmt (?=`'s'`) output format - * @param IMap to operate with another set of numbers * @returns `string | number | boolean` * @example * ```ts @@ -88,6 +80,6 @@ export type __Sub = export type Sub = N1 extends unknown ? N2 extends unknown - ? __Sub[0] + ? _Sub, IterationOf>[0] : never : never diff --git a/sources/Object/Assign.ts b/sources/Object/Assign.ts index 519689939..e5b040c49 100644 --- a/sources/Object/Assign.ts +++ b/sources/Object/Assign.ts @@ -8,7 +8,7 @@ import {Cast} from '../Any/Cast' import {List} from '../List/List' import {Extends} from '../Any/Extends' import {Depth} from './_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -32,15 +32,14 @@ export type _Assign, depth extends Dep * @param O to assign to * @param Os to assign * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param style (?=`1`) 0 = lodash, 1 = ramda - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`undefined`) types of `O` to be replaced with ones of `O1` * @returns [[Object]] * @example * ```ts * ``` */ -export type Assign, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type Assign, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = undefined> = O extends unknown ? Os extends unknown ? _Assign diff --git a/sources/Object/At.ts b/sources/Object/At.ts deleted file mode 100644 index d6eb99f71..000000000 --- a/sources/Object/At.ts +++ /dev/null @@ -1,51 +0,0 @@ -import {Key} from '../Any/Key' -import {Boolean} from '../Boolean/_Internal' - -/** - * @hidden - */ -export type AtBasic = - K extends keyof O - ? O[K] - : never - -/** - * @hidden - */ -export type AtStrict = - O[K & keyof O] // this is so that we can query `string | number` - -/** - * @hidden - */ -export type AtLoose = - O extends unknown - ? AtStrict - : never - -/** - * Get in `O` the type of a field of key `K` - * @param O to extract from - * @param K to extract at - * @param strict (?=`1`) `0` to work with unions - * @returns [[Any]] - * @example - * ```ts - * import {O} from 'ts-toolbelt' - * - * type User = { - * info: { - * name: string - * age: number - * payment: {} - * } - * id: number - * } - * - * type test0 = O.At // number - * ``` - */ -export type At = { - 1: AtStrict - 0: AtLoose -}[strict] diff --git a/sources/Object/AtLeast.ts b/sources/Object/AtLeast.ts index 7a413ed6c..b6bd72353 100644 --- a/sources/Object/AtLeast.ts +++ b/sources/Object/AtLeast.ts @@ -1,7 +1,7 @@ import {_Omit} from './Omit' import {_Pick} from './Pick' import {Key} from '../Any/Key' -import {Keys} from './Keys' +import {Keys} from '../Any/Keys' import {RequiredFlat} from './Required' import {Extends} from '../Any/Extends' import {ComputeRaw} from '../Any/Compute' diff --git a/sources/Object/Compulsory.ts b/sources/Object/Compulsory.ts index 40ee425c5..4b4059688 100644 --- a/sources/Object/Compulsory.ts +++ b/sources/Object/Compulsory.ts @@ -3,7 +3,7 @@ import {Depth} from './_Internal' import {Key} from '../Any/Key' import {NonNullable} from '../Union/NonNullable' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -16,7 +16,7 @@ export type CompulsoryFlat = { * @hidden */ export type CompulsoryDeep = { - [K in keyof O]-?: O[K] extends BuiltInObject + [K in keyof O]-?: O[K] extends BuiltIn ? O[K] : CompulsoryDeep> } diff --git a/sources/Object/ExcludeKeys.ts b/sources/Object/ExcludeKeys.ts index f538f5c56..2b01f8fe4 100644 --- a/sources/Object/ExcludeKeys.ts +++ b/sources/Object/ExcludeKeys.ts @@ -1,8 +1,8 @@ import {Exclude} from '../Union/Exclude' import {Match} from '../Any/_Internal' import {Is} from '../Any/Is' -import {At} from './At' -import {Keys} from './Keys' +import {At} from '../Any/At' +import {Keys} from '../Any/Keys' /** * @hidden diff --git a/sources/Object/Has.ts b/sources/Object/Has.ts index d191830f0..8c8a41fa6 100644 --- a/sources/Object/Has.ts +++ b/sources/Object/Has.ts @@ -1,6 +1,6 @@ import {Match} from '../Any/_Internal' import {Is} from '../Any/Is' -import {At} from './At' +import {At} from '../Any/At' import {Key} from '../Any/Key' /** diff --git a/sources/Object/IntersectKeys.ts b/sources/Object/IntersectKeys.ts index 8649208ca..2962cd293 100644 --- a/sources/Object/IntersectKeys.ts +++ b/sources/Object/IntersectKeys.ts @@ -1,8 +1,7 @@ -import {Intersect} from '../Union/Intersect' import {Match} from '../Any/_Internal' import {Is} from '../Any/Is' -import {At} from './At' -import {Keys} from './Keys' +import {At} from '../Any/At' +import {Keys} from '../Any/Keys' /** * @hidden @@ -33,7 +32,7 @@ type IntersectMatch = * ``` */ export type IntersectKeys = { - 'default' : Intersect, Keys> + 'default' : Keys & Keys 'contains->' : IntersectMatch'> 'extends->' : IntersectMatch'> '<-contains' : IntersectMatch diff --git a/sources/Object/Invert.ts b/sources/Object/Invert.ts index a26ddb177..b39eb5297 100644 --- a/sources/Object/Invert.ts +++ b/sources/Object/Invert.ts @@ -41,7 +41,7 @@ export type _Invert> = * type test1 = O.Invert * ``` */ -export type Invert> = +export type Invert> = O extends unknown ? _Invert : never diff --git a/sources/Object/Keys.ts b/sources/Object/Keys.ts deleted file mode 100644 index f47425b2a..000000000 --- a/sources/Object/Keys.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {Keys as UKeys} from '../Union/Keys' - -/** - * Get the keys of an [[Object]] - * @param O - * @returns [[Key]] - * @example - * ```ts - * ``` - */ -export type Keys = - UKeys diff --git a/sources/Object/ListOf.ts b/sources/Object/ListOf.ts index 1e17cd678..f41fdf80f 100644 --- a/sources/Object/ListOf.ts +++ b/sources/Object/ListOf.ts @@ -1,27 +1,28 @@ -import {IterationOf} from '../Iteration/IterationOf' -import {Iteration} from '../Iteration/Iteration' import {Cast} from '../Any/Cast' -import {Key} from '../Iteration/Key' -import {Next} from '../Iteration/Next' -import {Append} from '../List/Append' -import {Exclude} from '../Union/Exclude' import {List} from '../List/List' import {Extends} from '../Any/Extends' -import {At} from './At' +import {Select} from '../Union/Select' +import {Exclude} from '../Union/Exclude' +import {Iteration} from '../Iteration/Iteration' +import {IterationOf} from '../Iteration/IterationOf' +import {Pos} from '../Iteration/Pos' +import {Key} from '../Iteration/Key' +import {Append} from '../List/Append' +import {Next} from '../Iteration/Next' /** * @hidden */ -type PickIfEntry = - Key extends keyof O - ? Append & keyof O]> - : LN +type AppendExists = + Key extends keyof O ? Append]> : + Pos extends keyof O ? Append]> : + LN /** * @hidden */ type ___ListOf> = { - 0: ___ListOf>, PickIfEntry, Next> + 0: ___ListOf>, AppendExists, Next> 1: LN }[Extends<[K], [never]>] @@ -29,13 +30,10 @@ type ___ListOf = - number extends keyof O - ? At[] - : string extends keyof O - ? At[] - : symbol extends keyof O - ? At[] - : ___ListOf + number extends keyof O ? O[number][] : + string extends keyof O ? O[string][] : + symbol extends keyof O ? O[symbol][] : + ___ListOf> /** * @hidden diff --git a/sources/Object/Merge.ts b/sources/Object/Merge.ts index fb0eb3dfb..8fdbc2c1c 100644 --- a/sources/Object/Merge.ts +++ b/sources/Object/Merge.ts @@ -1,20 +1,21 @@ -import {AtBasic} from './At' +import {At} from '../Any/At' import {_OptionalKeys} from './OptionalKeys' import {Key} from '../Any/Key' import {List} from '../List/List' import {Depth, Anyfy} from './_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {Length} from '../List/Length' import {RequiredKeys} from '../List/RequiredKeys' import {Exclude} from '../Union/Exclude' +import {Has} from '../Union/Has' /** * @hidden */ type Longer = - [Exclude, RequiredKeys>] extends [never] - ? 1 - : 0 + L extends unknown ? L1 extends unknown ? + {0: 0, 1: 1}[Has, RequiredKeys>] + : never : never /** * @hidden @@ -22,15 +23,14 @@ type Longer = type MergeProp = K extends OOKeys // if prop of `O` is optional ? Exclude | O1K // merge it with prop of `O1` - : [OK] extends [never] // if it does not exist - ? O1K // complete with prop of `O1` - : OK extends fill ? O1K : OK // fill/replace if required + : [OK] extends [never] ? O1K : // complete with prop of `O1` + OK extends fill ? O1K : OK // fill/replace if required /** * @hidden */ type MergeFlatObject> = { - [K in keyof (Anyfy & O1)]: MergeProp, AtBasic, fill, OOKeys, K> + [K in keyof (Anyfy & O1)]: MergeProp, At, fill, OOKeys, K> } & {} /** @@ -40,36 +40,28 @@ type MergeFlatList ? MergeFlatChoice[] : Longer extends 1 - ? { // L is longer - [K in keyof L]: MergeProp, fill, LOK, K> - } - : { // L1 is longer - [K in keyof L1]: MergeProp, L1[K], fill, LOK, K> - } + ? {[K in keyof L]: MergeProp, fill, LOK, K>} + : {[K in keyof L1]: MergeProp, L1[K], fill, LOK, K>} /** * @hidden */ export type MergeFlatChoice = - O extends ignore - ? O - : O1 extends ignore - ? O - : O extends List - ? O1 extends List - ? MergeFlatList - : MergeFlatObject - : MergeFlatObject + O extends ignore ? O : + O1 extends ignore ? O : + O extends List + ? O1 extends List + ? MergeFlatList + : MergeFlatObject + : MergeFlatObject /** * @hidden */ -export type MergeFlat = - O extends unknown - ? O1 extends unknown - ? MergeFlatChoice - : never - : never +export type MergeFlat = + O extends unknown ? O1 extends unknown ? + MergeFlatChoice + : never : never /** * @hidden @@ -78,51 +70,41 @@ type MergeDeepList number extends Length ? MergeDeepChoice[] : Longer extends 1 - ? { // L is longer - [K in keyof L]: MergeDeepChoice, ignore, fill, _OptionalKeys, K> - } - : { // L1 is longer - [K in keyof L1]: MergeDeepChoice, L1[K], ignore, fill, _OptionalKeys, K> - } + ? {[K in keyof L]: MergeDeepChoice, ignore, fill, _OptionalKeys, K>} + : {[K in keyof L1]: MergeDeepChoice, L1[K], ignore, fill, _OptionalKeys, K>} /** * @hidden */ type MergeDeepObject> = { - [K in keyof (Anyfy & O1)]: MergeDeepChoice, AtBasic, ignore, fill, OOKeys, K> -} & {} + [K in keyof (Anyfy & O1)]: MergeDeepChoice, At, ignore, fill, OOKeys, K> +} /** * @hidden */ type MergeDeepChoice = - [OK] extends [never] - ? MergeProp - : [O1K] extends [never] - ? MergeProp - : OK extends ignore - ? MergeProp - : O1K extends ignore - ? MergeProp - : OK extends List - ? O1K extends List - ? MergeDeepList - : MergeProp - : OK extends object - ? O1K extends object - ? MergeDeepObject - : MergeProp - : MergeProp + [OK] extends [never] ? MergeProp : + [O1K] extends [never] ? MergeProp : + OK extends ignore ? MergeProp : + O1K extends ignore ? MergeProp : + OK extends List + ? O1K extends List + ? MergeDeepList + : MergeProp + : OK extends object + ? O1K extends object + ? MergeDeepObject + : MergeProp + : MergeProp /** * @hidden */ -export type MergeDeep = - O extends unknown - ? O1 extends unknown - ? MergeDeepChoice // dummy x, y - : never - : never +export type MergeDeep = + O extends unknown ? O1 extends unknown ? + MergeDeepChoice + : never : never /** * Accurately merge the fields of `O` with the ones of `O1`. It is @@ -133,9 +115,8 @@ export type MergeDeep = { +export type Merge = { 'flat': MergeFlat 'deep': MergeDeep }[depth] - -type mergeList = Merge<{a: [1]}, {a: [2, 3]}, 'deep'> diff --git a/sources/Object/MergeAll.ts b/sources/Object/MergeAll.ts index 8a3d5d32c..4cf0603f5 100644 --- a/sources/Object/MergeAll.ts +++ b/sources/Object/MergeAll.ts @@ -8,7 +8,7 @@ import {Cast} from '../Any/Cast' import {List} from '../List/List' import {Extends} from '../Any/Extends' import {Depth} from './_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -32,15 +32,14 @@ export type _MergeAll, depth extends D * @param O to start with * @param Os to merge * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param style (?=`1`) 0 = lodash, 1 = ramda - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`undefined`) types of `O` to be replaced with ones of `O1` * @returns [[Object]] * @example * ```ts * ``` */ -export type MergeAll, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type MergeAll, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = undefined> = O extends unknown ? Os extends unknown ? _MergeAll diff --git a/sources/Object/Modify.ts b/sources/Object/Modify.ts index be53412d1..3d7af8bcb 100644 --- a/sources/Object/Modify.ts +++ b/sources/Object/Modify.ts @@ -1,4 +1,4 @@ -import {At} from './At' +import {At} from '../Any/At' import {Replace} from '../Union/Replace' import {x} from '../Any/x' import {Exclude} from '../Union/Exclude' diff --git a/sources/Object/NonNullable.ts b/sources/Object/NonNullable.ts index 7e8a395fd..b20eece17 100644 --- a/sources/Object/NonNullable.ts +++ b/sources/Object/NonNullable.ts @@ -3,7 +3,7 @@ import {Depth} from './_Internal' import {_Pick} from './Pick' import {Key} from '../Any/Key' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -16,7 +16,7 @@ export type NonNullableFlat = { * @hidden */ export type NonNullableDeep = { - [K in keyof O]: O[K] extends BuiltInObject + [K in keyof O]: O[K] extends BuiltIn ? O[K] : NonNullableDeep> } diff --git a/sources/Object/Object.ts b/sources/Object/Object.ts index ccd61f57c..ae7a8e5b3 100644 --- a/sources/Object/Object.ts +++ b/sources/Object/Object.ts @@ -9,4 +9,5 @@ import {Record} from './Record' * type string1 = {b: "world"} * ``` */ -export type Object = Record +export type Object = + Record diff --git a/sources/Object/P/Merge.ts b/sources/Object/P/Merge.ts index 188c4191a..d7b30a7fa 100644 --- a/sources/Object/P/Merge.ts +++ b/sources/Object/P/Merge.ts @@ -1,43 +1,25 @@ -import {IterationOf} from '../../Iteration/IterationOf' -import {Iteration} from '../../Iteration/Iteration' -import {Pos} from '../../Iteration/Pos' -import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' import {Merge as OMerge} from '../Merge' -import {Length} from '../../List/Length' import {List} from '../../List/List' +import {BuiltIn} from '../../Misc/BuiltIn' +import {_ListOf} from '../ListOf' +import {Tail} from '../../List/Tail' import {Depth} from '../_Internal' -import {Boolean} from '../../Boolean/_Internal' /** * @hidden */ -type MergeObject, O1 extends object, depth extends Depth, I extends Iteration = IterationOf<0>> = - O extends object // If it's an object - ? Pos extends Length // If we've reached the end - ? OMerge // Use standard Merge - : { - [K in keyof O]: K extends Path[Pos] // If K is part of Path - ? MergeObject> // Continue diving - : O[K] // Not part of path - x - } & {} - : O // Not an object - x - -/** - * @hidden - */ -type MergeList, O1 extends object, depth extends Depth, I extends Iteration = IterationOf<0>> = - O extends object // Same as above, but - ? O extends (infer A)[] // If O is an array - ? MergeList[] // Dive into the array - : Pos extends Length - ? OMerge - : { - [K in keyof O]: K extends Path[Pos] - ? MergeList> - : O[K] - } & {} - : O +type MergeAt, O1, depth extends Depth> = + O extends BuiltIn ? O : + Path extends [] + ? O extends List ? OMerge : + O extends object ? OMerge : + O + : { + [K in keyof O]: K extends Path[0] + ? MergeAt, O1, depth> + : O[K] + } /** * Complete the fields of `O` at `Path` with the ones of `O1` @@ -45,13 +27,14 @@ type MergeList, O1 extends object, depth extends Depth * @param Path to be followed * @param O1 to copy from * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param list (?=`0`) `1` to work within object lists of arbitrary depth * @returns [[Object]] * @example * ```ts * ``` */ -export type Merge, O1 extends object, depth extends Depth = 'flat', list extends Boolean = 0> = { - 0: MergeObject - 1: MergeList -}[list] +export type Merge, O1 extends object, depth extends Depth = 'flat'> = + Path extends unknown + ? MergeAt + : never + +// TODO: swap path & o1 diff --git a/sources/Object/P/Omit.ts b/sources/Object/P/Omit.ts index 3e6db1ba0..924c6c592 100644 --- a/sources/Object/P/Omit.ts +++ b/sources/Object/P/Omit.ts @@ -1,54 +1,36 @@ -import {IterationOf} from '../../Iteration/IterationOf' -import {Iteration} from '../../Iteration/Iteration' -import {Pos} from '../../Iteration/Pos' -import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' import {_Omit as _OOmit} from '../Omit' -import {LastIndex} from '../../List/LastIndex' +import {_Omit as _LOmit} from '../../List/Omit' import {List} from '../../List/List' -import {Boolean} from '../../Boolean/_Internal' +import {BuiltIn} from '../../Misc/BuiltIn' +import {_ListOf} from '../ListOf' +import {Tail} from '../../List/Tail' /** * @hidden */ -type OmitObject, I extends Iteration = IterationOf<0>> = - O extends object // If it's an object - ? Pos extends LastIndex // If it's the last index - ? _OOmit]> // Use standard Omit - : { - [K in keyof O]: K extends Path[Pos] // If K is part of Path - ? OmitObject> // Continue diving - : O[K] // Not part of path - x - } & {} - : O // Not an object - x - -/** - * @hidden - */ -type OmitList, I extends Iteration = IterationOf<0>> = - O extends object // Same as above, but - ? O extends (infer A)[] // If O is an array - ? OmitList[] // Dive into the array - : Pos extends LastIndex - ? _OOmit]> - : { - [K in keyof O]: K extends Path[Pos] - ? OmitList> - : O[K] - } & {} - : O +type OmitAt> = + O extends BuiltIn ? O : + Path extends [Key] + ? O extends List ? _LOmit : + O extends object ? _OOmit : + O + : { + [K in keyof O]: K extends Path[0] + ? OmitAt> + : O[K] + } /** * Remove out of `O` the fields at `Path` * @param O to remove from * @param Path to be followed - * @param list (?=`0`) `1` to work within object lists of arbitrary depth * @returns [[Object]] * @example * ```ts * ``` */ -export type Omit, list extends Boolean = 0> = { - 0: OmitObject - 1: OmitList -}[list] +export type Omit> = + Path extends unknown + ? OmitAt + : never diff --git a/sources/Object/P/Pick.ts b/sources/Object/P/Pick.ts index 3ccdac208..5c2de839f 100644 --- a/sources/Object/P/Pick.ts +++ b/sources/Object/P/Pick.ts @@ -1,65 +1,37 @@ -import {IterationOf} from '../../Iteration/IterationOf' -import {Iteration} from '../../Iteration/Iteration' -import {Pos} from '../../Iteration/Pos' -import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' import {_Pick as _OPick} from '../Pick' import {_Pick as _LPick} from '../../List/Pick' -import {LastIndex} from '../../List/LastIndex' import {List} from '../../List/List' -import {Boolean} from '../../Boolean/_Internal' +import {Tail} from '../../List/Tail' +import {BuiltIn} from '../../Misc/BuiltIn' +import {_ListOf} from '../ListOf' /** * @hidden */ -type Action = - O extends List - ? number extends O['length'] - ? Action[] - : _LPick - : _OPick - -/** - * @hidden - */ -type PickObject, I extends Iteration = IterationOf<0>> = - O extends object // If it's an object - ? Action]> extends infer Picked // Pick the current index - ? Pos extends LastIndex // If it's the last index - ? Picked // Return the picked object - : { // Otherwise, continue diving - [K in keyof Picked]: PickObject> - } & {} - : never - : O // Not an object - x - -/** - * @hidden - */ -type PickList, I extends Iteration = IterationOf<0>> = - O extends object // Same as above, but - ? O extends (infer A)[] // If O is an array - ? PickList[] // Dive into the array - : _OPick]> extends infer Picked - ? Pos extends LastIndex - ? Picked - : { - [K in keyof Picked]: PickList> - } & {} - : never - : O +type PickAt> = + [] extends Path ? O : + O extends BuiltIn ? O : + O extends List ? _ListOf<{ + [K in keyof _OPick]: + PickAt> + }> : + O extends object ? { + [K in keyof _OPick]: + PickAt> + } : O /** * Extract out of `O` the fields at `Path` * @param O to extract from * @param Path to be followed - * @param list (?=`0`) `1` to work within object lists of arbitrary depth * @returns [[Object]] * @example * ```ts * ``` */ -export type Pick, list extends Boolean = 0> = { - 0: PickObject - 1: PickList -}[list] +export type Pick> = + Path extends unknown + ? PickAt + : never + diff --git a/sources/Object/P/Readonly.ts b/sources/Object/P/Readonly.ts index d87e6e497..1a434612e 100644 --- a/sources/Object/P/Readonly.ts +++ b/sources/Object/P/Readonly.ts @@ -1,56 +1,37 @@ -import {IterationOf} from '../../Iteration/IterationOf' -import {Iteration} from '../../Iteration/Iteration' -import {Pos} from '../../Iteration/Pos' -import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' import {Readonly as OReadonly} from '../Readonly' -import {LastIndex} from '../../List/LastIndex' import {List} from '../../List/List' +import {BuiltIn} from '../../Misc/BuiltIn' +import {_ListOf} from '../ListOf' +import {Tail} from '../../List/Tail' import {Depth} from '../_Internal' -import {Boolean} from '../../Boolean/_Internal' /** * @hidden */ -type ReadonlyObject, depth extends Depth, I extends Iteration = IterationOf<0>> = - O extends object // If it's an object - ? Pos extends LastIndex // If it's the last index - ? OReadonly], depth> // Use standard ReadOnly - : { - [K in keyof O]: K extends Path[Pos] // If K is part of Path - ? ReadonlyObject> // Continue diving - : O[K] // Not part of path - x - } & {} - : O // Not an object - x - -/** - * @hidden - */ -type ReadonlyArrays, depth extends Depth, I extends Iteration = IterationOf<0>> = - O extends object // Same as above, but - ? O extends (infer A)[] // If O is an array - ? ReadonlyArrays[] // Dive into the array - : Pos extends LastIndex - ? OReadonly], depth> - : { - [K in keyof O]: K extends Path[Pos] - ? ReadonlyArrays> - : O[K] - } & {} - : O +type ReadonlyAt, depth extends Depth> = + O extends BuiltIn ? O : + Path extends [Key] + ? O extends List ? OReadonly : + O extends object ? OReadonly : + O + : { + [K in keyof O]: K extends Path[0] + ? ReadonlyAt, depth> + : O[K] + } /** * Make some fields of `O` readonly at `Path` (deeply or not) * @param O to make readonly * @param Path to be followed * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param list (?=`0`) `1` to work within object lists of arbitrary depth * @returns [[Object]] * @example * ```ts * ``` */ -export type Readonly, depth extends Depth = 'flat', list extends Boolean = 0> = { - 0: ReadonlyObject - 1: ReadonlyArrays -}[list] +export type Readonly, depth extends Depth = 'flat'> = + Path extends unknown + ? ReadonlyAt + : never diff --git a/sources/Object/P/Record.ts b/sources/Object/P/Record.ts index 958910b21..e27e98be6 100644 --- a/sources/Object/P/Record.ts +++ b/sources/Object/P/Record.ts @@ -4,14 +4,14 @@ import {Iteration} from '../../Iteration/Iteration' import {Pos} from '../../Iteration/Pos' import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' -import {LastIndex} from '../../List/LastIndex' +import {LastKey} from '../../List/LastKey' import {List} from '../../List/List' /** * @hidden */ type Record_RR, A, I extends Iteration = IterationOf<0>> = { - readonly [Key in Path[Pos]]: Pos extends LastIndex + readonly [Key in Path[Pos]]: Pos extends LastKey ? A : Record_RR> } & {} @@ -20,7 +20,7 @@ type Record_RR, A, I extends Iteration = IterationOf<0>> * @hidden */ export type Record_RW, A, I extends Iteration = IterationOf<0>> = { - [Key in Path[Pos]]: Pos extends LastIndex + [Key in Path[Pos]]: Pos extends LastKey ? A : Record_RW> } & {} @@ -29,7 +29,7 @@ export type Record_RW, A, I extends Iteration = Iteration * @hidden */ type Record_OR, A, I extends Iteration = IterationOf<0>> = { - readonly [Key in Path[Pos]]?: Pos extends LastIndex + readonly [Key in Path[Pos]]?: Pos extends LastKey ? A : Record_OR> } & {} @@ -38,7 +38,7 @@ type Record_OR, A, I extends Iteration = IterationOf<0>> * @hidden */ type Record_OW, A, I extends Iteration = IterationOf<0>> = { - [Key in Path[Pos]]?: Pos extends LastIndex + [Key in Path[Pos]]?: Pos extends LastKey ? A : Record_OW> } & {} diff --git a/sources/Object/P/Update.ts b/sources/Object/P/Update.ts index d2b81beb2..5ad486544 100644 --- a/sources/Object/P/Update.ts +++ b/sources/Object/P/Update.ts @@ -1,28 +1,26 @@ -import {IterationOf} from '../../Iteration/IterationOf' -import {Iteration} from '../../Iteration/Iteration' -import {Pos} from '../../Iteration/Pos' -import {Next} from '../../Iteration/Next' import {Key} from '../../Any/Key' -import {LastIndex} from '../../List/LastIndex' +import {Update as OUpdate} from '../Update' +import {Update as LUpdate} from '../../List/Update' import {List} from '../../List/List' +import {BuiltIn} from '../../Misc/BuiltIn' +import {_ListOf} from '../ListOf' +import {Tail} from '../../List/Tail' import {Record} from '../Record' -import {Patch} from '../Patch' /** * @hidden */ -type UpdateObject, A, I extends Iteration = IterationOf<0>> = - O extends object // if it's an object - ? Pos extends LastIndex // if it's the last index - ? Patch], A>, O> // use standard Update - : (O & Record], keyof O>, {}>) extends infer O // fill in missing keys with non-object - ? { // to effectively build the object up - [K in keyof O]: K extends Path[Pos] // if K is part of path - ? UpdateObject> // keep diving - : O[K] // not part of path - x - } & {} - : never - : O +type UpdateAt, A> = + O extends BuiltIn ? O : + Path extends [Key] + ? O extends List ? LUpdate : + O extends object ? OUpdate : + O + : { + [K in keyof O]: K extends Path[0] + ? UpdateAt, A> + : O[K] + } /** * Update in `O` the fields at `Path` with `A` @@ -35,4 +33,6 @@ type UpdateObject, A, I extends Iteration = IterationO * ``` */ export type Update, A extends any> = - UpdateObject + Path extends unknown + ? UpdateAt + : never diff --git a/sources/Object/Patch.ts b/sources/Object/Patch.ts index a22e3ef27..497c90bba 100644 --- a/sources/Object/Patch.ts +++ b/sources/Object/Patch.ts @@ -1,19 +1,20 @@ -import {AtBasic} from './At' +import {At} from '../Any/At' import {Key} from '../Any/Key' import {_ListOf} from './ListOf' import {List} from '../List/List' import {Depth} from './_Internal' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' import {_Omit} from './Omit' import {Length} from '../List/Length' +import {Has} from '../Union/Has' /** * @hidden */ type Longer = - [Exclude] extends [never] - ? 1 - : 0 + L extends unknown ? L1 extends unknown ? + {0: 0, 1: 1}[Has] + : never : never /** * @hidden @@ -27,7 +28,7 @@ type PatchProp = * @hidden */ type PatchFlatObject = { - [K in keyof (O & _Omit)]: PatchProp, AtBasic, fill, OKeys, K> + [K in keyof (O & _Omit)]: PatchProp, At, fill, OKeys, K> } & {} /** @@ -37,36 +38,28 @@ type PatchFlatList number extends Length ? PatchFlatChoice[] : Longer extends 1 - ? { // L is longer - [K in keyof L]: PatchProp, fill, keyof L, K> - } - : { // L1 is longer - [K in keyof L1]: PatchProp, L1[K], fill, keyof L, K> - } + ? {[K in keyof L]: PatchProp, fill, keyof L, K>} + : {[K in keyof L1]: PatchProp, L1[K], fill, keyof L, K>} /** * @hidden */ export type PatchFlatChoice = - O extends ignore - ? O - : O1 extends ignore - ? O - : O extends List - ? O1 extends List - ? PatchFlatList - : PatchFlatObject - : PatchFlatObject + O extends ignore ? O : + O1 extends ignore ? O : + O extends List + ? O1 extends List + ? PatchFlatList + : PatchFlatObject + : PatchFlatObject /** * @hidden */ -export type PatchFlat = - O extends unknown - ? O1 extends unknown - ? PatchFlatChoice - : never - : never +export type PatchFlat = + O extends unknown ? O1 extends unknown ? + PatchFlatChoice + : never : never /** * @hidden @@ -75,62 +68,51 @@ type PatchDeepList number extends Length ? PatchDeepChoice[] : Longer extends 1 - ? { // L is longer - [K in keyof L]: PatchDeepChoice, ignore, fill, keyof L, K> - } - : { // L1 is longer - [K in keyof L1]: PatchDeepChoice, L1[K], ignore, fill, keyof L, K> - } + ? {[K in keyof L]: PatchDeepChoice, ignore, fill, keyof L, K>} + : {[K in keyof L1]: PatchDeepChoice, L1[K], ignore, fill, keyof L, K>} /** * @hidden */ type PatchDeepObject = { - [K in keyof (O & _Omit)]: PatchDeepChoice, AtBasic, ignore, fill, OKeys, K> -} & {} + [K in keyof (O & _Omit)]: PatchDeepChoice, At, ignore, fill, OKeys, K> +} /** * @hidden */ type PatchDeepChoice = - [OK] extends [never] - ? PatchProp - : [O1K] extends [never] - ? PatchProp - : OK extends ignore - ? PatchProp - : O1K extends ignore - ? PatchProp - : OK extends List - ? O1K extends List - ? PatchDeepList - : PatchProp - : OK extends object - ? O1K extends object - ? PatchDeepObject - : PatchProp - : PatchProp + [OK] extends [never] ? PatchProp : + [O1K] extends [never] ? PatchProp : + OK extends ignore ? PatchProp : + O1K extends ignore ? PatchProp : + OK extends List + ? O1K extends List + ? PatchDeepList + : PatchProp + : OK extends object + ? O1K extends object + ? PatchDeepObject + : PatchProp + : PatchProp /** * @hidden */ -export type PatchDeep = - O extends unknown - ? O1 extends unknown - ? PatchDeepChoice // dummy x, y - : never - : never +export type PatchDeep = + O extends unknown ? O1 extends unknown ? + PatchDeepChoice // dummy x, y + : never : never /** * Complete the fields of `O` with the ones of `O1`. This is a version of - * [[Merge]] that does NOT handle optional fields, it only completes fields of `O` - * with the ones of `O1`. + * [[Merge]] that does NOT handle optional fields, it only completes fields of + * `O` with the ones of `O1`. * @param O to complete * @param O1 to copy from * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param style (?=`1`) 0 = lodash, 1 = ramda - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`never`) types of `O` to be replaced with ones of `O1` * @returns [[Object]] * @example * ```ts @@ -168,7 +150,7 @@ export type PatchDeep = { +export type Patch = { 'flat': PatchFlat 'deep': PatchDeep }[depth] diff --git a/sources/Object/PatchAll.ts b/sources/Object/PatchAll.ts index d7c072cdd..835629cb8 100644 --- a/sources/Object/PatchAll.ts +++ b/sources/Object/PatchAll.ts @@ -8,7 +8,7 @@ import {List} from '../List/List' import {Extends} from '../Any/Extends' import {Depth} from './_Internal' import {Patch} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -32,15 +32,14 @@ export type _PatchAll, depth extends D * @param O to start with * @param Os to patch * @param depth (?=`'flat'`) 'deep' to do it deeply - * @param style (?=`1`) 0 = lodash, 1 = ramda - * @param ignore (?=`BuiltinObject`) types not to merge - * @param fill (?=`fill`) types of `O` to be replaced with ones of `O1` + * @param ignore (?=`BuiltIn`) types not to merge + * @param fill (?=`never`) types of `O` to be replaced with ones of `O1` * @returns [[Object]] * @example * ```ts * ``` */ -export type PatchAll, depth extends Depth = 'flat', ignore extends object = BuiltInObject, fill extends any = never> = +export type PatchAll, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = never> = O extends unknown ? Os extends unknown ? _PatchAll diff --git a/sources/Object/Path.ts b/sources/Object/Path.ts index 508888f43..42896f478 100644 --- a/sources/Object/Path.ts +++ b/sources/Object/Path.ts @@ -1,45 +1,32 @@ -import {IterationOf} from '../Iteration/IterationOf' +import {Key} from '../Any/Key' +import {Cast} from '../Any/Cast' +import {Extends} from '../Any/Extends' import {Iteration} from '../Iteration/Iteration' +import {IterationOf} from '../Iteration/IterationOf' import {Next} from '../Iteration/Next' import {Pos} from '../Iteration/Pos' -import {Length} from '../List/Length' -import {At} from './At' -import {Cast} from '../Any/Cast' -import {NonNullable} from '../Union/NonNullable' -import {Key} from '../Any/Key' import {List} from '../List/List' -import {Boolean} from '../Boolean/_Internal' -import {Extends} from '../Any/Extends' +import {Length} from '../List/Length' +import {At} from '../Any/At' /** - * @hidden + * @ignore */ -type __Path, strict extends Boolean, I extends Iteration = IterationOf<0>> = { - 0: __Path & {}, Path[Pos], strict>, Path, strict, Next> - // Use of `NonNullable` otherwise path cannot be followed #`undefined` +type _Path, I extends Iteration = IterationOf<0>> = { + 0: _Path]>, P, Next> 1: O -}[Extends, Length>] - -/** - * @hidden - */ -export type _Path, strict extends Boolean> = - __Path extends infer X - ? Cast - : never +}[Extends, Length

>] /** * Get in `O` the type of nested properties * @param O to be inspected * @param Path to be followed - * @param strict (?=`1`) `0` to work with unions * @returns [[Any]] * @example * ```ts * ``` */ -export type Path, strict extends Boolean = 1> = - //! O extends unknown // not needed, this is what strict = 0 does - Path extends unknown - ? _Path +export type Path> = + _Path extends infer X + ? Cast : never diff --git a/sources/Object/Paths.ts b/sources/Object/Paths.ts index 7bffb762a..878ff0056 100644 --- a/sources/Object/Paths.ts +++ b/sources/Object/Paths.ts @@ -1,37 +1,30 @@ -import {OptionalFlat} from '../Object/Optional' import {Key} from '../Any/Key' import {NonNullableFlat} from '../Object/NonNullable' -import {Concat} from '../List/Concat' import {Cast} from '../Any/Cast' -import {Equals} from '../Any/Equals' import {List} from '../List/List' -import {Append} from '../List/Append' +import {BuiltIn} from '../Misc/BuiltIn' +import {Primitive} from '../Misc/Primitive' +import {Length} from '../List/Length' +import {Keys} from '../Any/Keys' /** * @hidden */ -type __Paths = []> = { - 0: {[K in keyof O]: __Paths>}[keyof O] - // It dives deep, and as it dives, it adds the paths to `Paths` - 1: NonNullableFlat> - 2: NonNullableFlat>> -}[ - Equals extends 1 // Handle infinite recursion - ? 2 // 1: Exit adding infinite Path - : O extends object // 0: > If object - ? [keyof O] extends [never] // & If recursion has finished - ? 1 // 1: Exit - : 0 // 0: Continue - : 1 // 1: Exit -] +type UnionOf = + A extends List + ? A[number] + : A[keyof A] /** * @hidden */ -export type _Paths = - __Paths extends infer X - ? Cast> - : never +type _Paths = UnionOf<{ + [K in keyof O]: + O[K] extends BuiltIn | Primitive ? NonNullableFlat<[...P, K?]> : + [Keys] extends [never] ? NonNullableFlat<[...P, K?]> : + 12 extends Length

? NonNullableFlat<[...P, K?]> : + _Paths +}> /** * Get all the possible paths of `O` @@ -42,7 +35,7 @@ export type _Paths = * ```ts * ``` */ -export type Paths = - O extends unknown - ? _Paths +export type Paths = + _Paths extends infer X + ? Cast> : never diff --git a/sources/Object/Readonly.ts b/sources/Object/Readonly.ts index 3dad6b0d6..3432f6744 100644 --- a/sources/Object/Readonly.ts +++ b/sources/Object/Readonly.ts @@ -2,7 +2,7 @@ import {_Pick} from './Pick' import {Depth} from './_Internal' import {Key} from '../Any/Key' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -15,7 +15,7 @@ export type ReadonlyFlat = { * @hidden */ export type ReadonlyDeep = { - +readonly [K in keyof O]: O[K] extends BuiltInObject + +readonly [K in keyof O]: O[K] extends BuiltIn ? O[K] : ReadonlyDeep } diff --git a/sources/Object/Record.ts b/sources/Object/Record.ts index 426047457..4b149d9fd 100644 --- a/sources/Object/Record.ts +++ b/sources/Object/Record.ts @@ -4,14 +4,14 @@ import {Key} from '../Any/Key' /** * Create an object filled with `A` for the fields `K` * @param K to choose fields - * @param A to fill fields with + * @param A (?=`unknown`) to fill fields with * @param modx (?=`['!', 'W']`) to set modifiers * @returns [[Object]] * @example * ```ts * ``` */ -export type Record = { +export type Record = { '!': { 'R': {readonly [P in K]: A} 'W': { [P in K]: A} diff --git a/sources/Object/Required.ts b/sources/Object/Required.ts index 50f641001..350c3c5b4 100644 --- a/sources/Object/Required.ts +++ b/sources/Object/Required.ts @@ -2,7 +2,7 @@ import {_Pick} from './Pick' import {Depth} from './_Internal' import {Key} from '../Any/Key' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -15,7 +15,7 @@ export type RequiredFlat = { * @hidden */ export type RequiredDeep = { - [K in keyof O]-?: O[K] extends BuiltInObject + [K in keyof O]-?: O[K] extends BuiltIn ? O[K] : RequiredDeep } diff --git a/sources/Object/Undefinable.ts b/sources/Object/Undefinable.ts index ae116811c..c668227bd 100644 --- a/sources/Object/Undefinable.ts +++ b/sources/Object/Undefinable.ts @@ -2,7 +2,7 @@ import {Depth} from './_Internal' import {_Pick} from './Pick' import {Key} from '../Any/Key' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -15,7 +15,7 @@ export type UndefinableFlat = { * @hidden */ export type UndefinableDeep = { - [K in keyof O]: O[K] extends BuiltInObject + [K in keyof O]: O[K] extends BuiltIn ? O[K] : UndefinableDeep } diff --git a/sources/Object/UnionOf.ts b/sources/Object/UnionOf.ts index fc49e1e66..3a210e97e 100644 --- a/sources/Object/UnionOf.ts +++ b/sources/Object/UnionOf.ts @@ -1,10 +1,8 @@ -import {At} from './At' - /** * @hidden */ export type _UnionOf = - At + O[keyof O] /** * Transform an [[Object]] into an [[Union]] diff --git a/sources/Object/Unionize.ts b/sources/Object/Unionize.ts index 26487aac4..d030364f1 100644 --- a/sources/Object/Unionize.ts +++ b/sources/Object/Unionize.ts @@ -1,4 +1,4 @@ -import {At} from './At' +import {At} from '../Any/At' import {Key} from '../Any/Key' /** diff --git a/sources/Object/Update.ts b/sources/Object/Update.ts index 587200452..bb1e59d22 100644 --- a/sources/Object/Update.ts +++ b/sources/Object/Update.ts @@ -1,40 +1,6 @@ import {Key} from '../Any/Key' import {x} from '../Any/x' import {Replace} from '../Union/Replace' -import {Depth} from './_Internal' - -/** - * @hidden - */ -type UpdateFlat = { - [P in keyof O]: P extends K - ? Replace - : O[P] -} & {} - -/** - * @hidden - */ -type __UpdateDeep = { - [K in keyof O]: Replace extends infer X - ? X extends object - ? __UpdateDeep - : X - : never -} - -/** - * @hidden - */ -type _UpdateDeep> = { - [K in keyof OU]: __UpdateDeep -} & {} - -/** - * @hidden - */ -export type UpdateDeep = - _UpdateDeep /** * Update in `O` the fields of key `K` with `A`. @@ -67,7 +33,8 @@ export type UpdateDeep = * // } * ``` */ -export type Update = { - 'flat': UpdateFlat - 'deep': UpdateDeep -}[depth] +export type Update = { + [P in keyof O]: P extends K + ? Replace + : O[P] +} & {} diff --git a/sources/Object/Writable.ts b/sources/Object/Writable.ts index ebbd56809..e7b0f6996 100644 --- a/sources/Object/Writable.ts +++ b/sources/Object/Writable.ts @@ -2,7 +2,7 @@ import {_Pick} from './Pick' import {Depth} from './_Internal' import {Key} from '../Any/Key' import {PatchFlat} from './Patch' -import {BuiltInObject} from '../Misc/BuiltInObject' +import {BuiltIn} from '../Misc/BuiltIn' /** * @hidden @@ -15,7 +15,7 @@ export type WritableFlat = { * @hidden */ export type WritableDeep = { - -readonly [K in keyof O]: O[K] extends BuiltInObject + -readonly [K in keyof O]: O[K] extends BuiltIn ? O[K] : WritableDeep } diff --git a/sources/Object/_Internal.ts b/sources/Object/_Internal.ts index 4b43f9716..f1d5733e2 100644 --- a/sources/Object/_Internal.ts +++ b/sources/Object/_Internal.ts @@ -27,4 +27,4 @@ export type MergeStyle = 0 | 1 | 2 */ export type Anyfy = { [K in keyof O]: any -} & {} +} diff --git a/sources/Object/_api.ts b/sources/Object/_api.ts index e2227da01..810f7f700 100644 --- a/sources/Object/_api.ts +++ b/sources/Object/_api.ts @@ -4,7 +4,6 @@ import * as P from './P/_api' export {P} export {Assign} from './Assign' -export {At} from './At' export {AtLeast} from './AtLeast' export {Compulsory} from './Compulsory' export {CompulsoryKeys} from './CompulsoryKeys' @@ -20,7 +19,6 @@ export {Includes} from './Includes' export {Intersect} from './Intersect' export {IntersectKeys} from './IntersectKeys' export {Invert} from './Invert' -export {Keys} from './Keys' export {ListOf} from './ListOf' export {Merge} from './Merge' export {MergeAll} from './MergeAll' @@ -39,7 +37,6 @@ export {Patch} from './Patch' export {PatchAll} from './PatchAll' export {Path} from './Path' export {Paths} from './Paths' -export {PathValid} from './PathValid' export {Pick} from './Pick' export {Readonly} from './Readonly' export {ReadonlyKeys} from './ReadonlyKeys' diff --git a/sources/String/At.ts b/sources/String/At.ts new file mode 100644 index 000000000..a6cc32ff1 --- /dev/null +++ b/sources/String/At.ts @@ -0,0 +1,11 @@ +import {Split} from './Split' + +import {At as AAt} from '../Any/At' + +/** + * Get the character at position `K` + * @param S + * @param K + */ +export type At = + AAt, K> diff --git a/sources/String/Join.ts b/sources/String/Join.ts new file mode 100644 index 000000000..130a57d53 --- /dev/null +++ b/sources/String/Join.ts @@ -0,0 +1,22 @@ +import {List} from '../List/List' +import {Literal} from './_Internal' +import {Cast} from '../Any/Cast' + +/** + * @hidden + */ +type _Join = + T extends [] ? '' : + T extends [Literal] ? `${T[0]}` : + T extends [Literal, ...infer R] ? `${T[0]}${D}${_Join}` : + string + +/** + * Concat many literals together + * @param T to concat + * @param D to delimit + */ +export type Join, D extends string = ''> = + _Join extends infer X + ? Cast + : never diff --git a/sources/String/Length.ts b/sources/String/Length.ts new file mode 100644 index 000000000..616b9b741 --- /dev/null +++ b/sources/String/Length.ts @@ -0,0 +1,9 @@ +import {Split} from './Split' +import {Length as LLength} from '../List/Length' + +/** + * Get the length of a `string` + * @param S + */ +export type Length = + LLength> diff --git a/sources/String/Replace.ts b/sources/String/Replace.ts new file mode 100644 index 000000000..38099eca9 --- /dev/null +++ b/sources/String/Replace.ts @@ -0,0 +1,21 @@ +import {Cast} from '../Any/Cast' +import {Literal} from './_Internal' + +/** + * @hidden + */ +type _Replace = + S extends `${infer BS}${R}${infer AS}` + ? Replace<`${BS}${W}${AS}`, R, W> + : S + +/** + * Replace `R` with `W` in `S` + * @param S + * @param R + * @param W + */ +export type Replace = + _Replace extends infer X + ? Cast + : never diff --git a/sources/String/Split.ts b/sources/String/Split.ts index e69de29bb..4fdbaf0bc 100644 --- a/sources/String/Split.ts +++ b/sources/String/Split.ts @@ -0,0 +1,26 @@ +import {Cast} from '../Any/Cast' +import {Pop} from '../List/Pop' + +/** + * @ignore + */ +type __Split = + S extends `${infer BS}${D}${infer AS}` + ? __Split + : [...T, S] + +/** + * @hidden + */ +type _Split = + D extends '' ? Pop<__Split> : __Split + +/** + * Split `S` by `D` into a [[List]] + * @param S to split up + * @param D to split at + */ +export type Split = + _Split extends infer X + ? Cast + : never diff --git a/sources/String/_Internal.ts b/sources/String/_Internal.ts index e69de29bb..1dee40a86 100644 --- a/sources/String/_Internal.ts +++ b/sources/String/_Internal.ts @@ -0,0 +1 @@ +export type Literal = string | number | bigint | boolean diff --git a/sources/String/_api.ts b/sources/String/_api.ts index 469539985..b12f447ab 100644 --- a/sources/String/_api.ts +++ b/sources/String/_api.ts @@ -1,3 +1,7 @@ /** @ignore *//** */ -export {} +export {At} from './At' +export {Join} from './Join' +export {Length} from './Length' +export {Replace} from './Replace' +export {Split} from './Split' diff --git a/sources/Union/Diff.ts b/sources/Union/Diff.ts index e22b809ca..552c780c5 100644 --- a/sources/Union/Diff.ts +++ b/sources/Union/Diff.ts @@ -1,5 +1,4 @@ import {Exclude} from './Exclude' -import {Union} from './Union' /** * Get an [[Union]] that is the difference between `U1` & `U2` @@ -10,5 +9,5 @@ import {Union} from './Union' * ```ts * ``` */ -export type Diff = +export type Diff = Exclude | Exclude diff --git a/sources/Union/Exclude.ts b/sources/Union/Exclude.ts index ececb13e8..ddad8def5 100644 --- a/sources/Union/Exclude.ts +++ b/sources/Union/Exclude.ts @@ -1,5 +1,3 @@ -import {Union} from './Union' - /** * Remove `M` out of `U` * @param U to remove from @@ -9,7 +7,7 @@ import {Union} from './Union' * ```ts * ``` */ -export type Exclude = +export type Exclude = U extends M ? never : U diff --git a/sources/Union/Filter.ts b/sources/Union/Filter.ts index 28cbe2f58..8a9ce43d6 100644 --- a/sources/Union/Filter.ts +++ b/sources/Union/Filter.ts @@ -1,4 +1,3 @@ -import {Union} from './Union' import {Match} from '../Any/_Internal' import {Is} from '../Any/Is' @@ -11,9 +10,7 @@ import {Is} from '../Any/Is' * ```ts * ``` */ -export type Filter = +export type Filter = U extends unknown - ? Is extends 1 - ? never - : U + ? {1: never, 0: U & M}[Is] : never diff --git a/sources/Union/Has.ts b/sources/Union/Has.ts index ea38bc228..b964f2e86 100644 --- a/sources/Union/Has.ts +++ b/sources/Union/Has.ts @@ -1,5 +1,3 @@ -import {Union} from './Union' - /** * Check whether `U` contains `U1` * @param U to be inspected @@ -9,7 +7,7 @@ import {Union} from './Union' * ```ts * ``` */ -export type Has = +export type Has = [U1] extends [U] ? 1 : 0 diff --git a/sources/Union/Intersect.ts b/sources/Union/Intersect.ts index 3e4c57c86..42f4aec11 100644 --- a/sources/Union/Intersect.ts +++ b/sources/Union/Intersect.ts @@ -1,13 +1,18 @@ -import {Union} from './Union' +import {Equals} from '../Any/_api' +import type {Select} from './Select' /** - * Get the intersection of `U1` & `U2` - * @param U1 to check similarities with - * @param U2 to check similarities against + * Get the overlapping members of `U1` and `U2` + * @param U1 + * @param U2 * @returns [[Union]] * @example * ```ts * ``` */ -export type Intersect = - U1 & U2 +export type Intersect = + U1 extends unknown + ? U2 extends unknown + ? {1: U1, 0: never}[Equals] + : never + : never diff --git a/sources/Union/IntersectOf.ts b/sources/Union/IntersectOf.ts index c3b35a786..dae9e4beb 100644 --- a/sources/Union/IntersectOf.ts +++ b/sources/Union/IntersectOf.ts @@ -1,5 +1,3 @@ -import {Union} from './Union' - // Credit https://stackoverflow.com/a/50375286/3570903 /** @@ -10,7 +8,7 @@ import {Union} from './Union' * ```ts * ``` */ -export type IntersectOf = +export type IntersectOf = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never diff --git a/sources/Union/Keys.ts b/sources/Union/Keys.ts deleted file mode 100644 index 8cfb4babc..000000000 --- a/sources/Union/Keys.ts +++ /dev/null @@ -1,14 +0,0 @@ -import {Union} from './Union' - -/** - * Get the keys of a [[Union]] - * @param U - * @returns [[Key]] - * @example - * ```ts - * ``` - */ -export type Keys = - U extends unknown - ? keyof U - : never diff --git a/sources/Union/Last.ts b/sources/Union/Last.ts index cb8de5c0a..5ed68a931 100644 --- a/sources/Union/Last.ts +++ b/sources/Union/Last.ts @@ -1,5 +1,4 @@ import {IntersectOf} from './IntersectOf' -import {Union} from './Union' /** * Get the last item within an [[Union]] @@ -10,7 +9,7 @@ import {Union} from './Union' * ```ts * ``` */ -export type Last = +export type Last = IntersectOf< U extends unknown // Distribute U ? (x: U) => void @@ -21,5 +20,5 @@ export type Last = /* * The above does this (i.e.) - * ParamsOf<((a: 1) => void) & ((a: 2) => void)> // => [2] + * Parameters<((a: 1) => void) & ((a: 2) => void)> // => [2] */ diff --git a/sources/Union/ListOf.ts b/sources/Union/ListOf.ts index 699325990..4c4b01cb7 100644 --- a/sources/Union/ListOf.ts +++ b/sources/Union/ListOf.ts @@ -2,7 +2,6 @@ import {Last} from './Last' import {Prepend} from '../List/Prepend' import {Exclude} from './Exclude' import {List} from '../List/List' -import {Union} from './Union' import {Cast} from '../Any/Cast' import {Extends} from '../Any/Extends' @@ -23,7 +22,7 @@ type _ListOf> = { * ```ts * ``` */ -export type ListOf = +export type ListOf = _ListOf extends infer X ? Cast : never diff --git a/sources/Union/Merge.ts b/sources/Union/Merge.ts index 27b84664f..06b9a0d33 100644 --- a/sources/Union/Merge.ts +++ b/sources/Union/Merge.ts @@ -1,4 +1,4 @@ -import {At} from '../Object/At' +import {At} from '../Any/At' import {Overwrite} from '../Object/Overwrite' import {ComputeRaw} from '../Any/Compute' import {IntersectOf} from './IntersectOf' diff --git a/sources/Union/NonNullable.ts b/sources/Union/NonNullable.ts index 36e530b99..6afcf4423 100644 --- a/sources/Union/NonNullable.ts +++ b/sources/Union/NonNullable.ts @@ -1,5 +1,5 @@ import {Exclude} from './Exclude' -import {Union} from './Union' + /** * Remove `undefined` & `null` out of `U` @@ -9,5 +9,5 @@ import {Union} from './Union' * ```ts * ``` */ -export type NonNullable = +export type NonNullable = Exclude diff --git a/sources/Union/Nullable.ts b/sources/Union/Nullable.ts index 89cd8adf7..c5da0b9ca 100644 --- a/sources/Union/Nullable.ts +++ b/sources/Union/Nullable.ts @@ -1,5 +1,3 @@ -import {Union} from './Union' - /** * Add `undefined | null` to `U` * @param U to make nullable @@ -8,5 +6,5 @@ import {Union} from './Union' * ```ts * ``` */ -export type Nullable = +export type Nullable = U | undefined | null diff --git a/sources/Union/Pop.ts b/sources/Union/Pop.ts index e9f05b354..ae2d26de7 100644 --- a/sources/Union/Pop.ts +++ b/sources/Union/Pop.ts @@ -1,6 +1,5 @@ import {Exclude} from './Exclude' import {Last} from './Last' -import {Union} from './Union' /** * Remove an item out of `U` @@ -11,5 +10,5 @@ import {Union} from './Union' * ```ts * ``` */ -export type Pop = +export type Pop = Exclude> diff --git a/sources/Union/Replace.ts b/sources/Union/Replace.ts index a568dad16..2ebe9ea1b 100644 --- a/sources/Union/Replace.ts +++ b/sources/Union/Replace.ts @@ -1,4 +1,5 @@ -import {Union} from './Union' +import {Is} from '../Any/Is' +import {Match} from '../Any/_Internal' /** * Replace `M` with `A` in `U` @@ -10,7 +11,7 @@ import {Union} from './Union' * ```ts * ``` */ -export type Replace = - U extends M - ? A - : U +export type Replace = + U extends unknown + ? {1: A, 0: U}[Is] + : never diff --git a/sources/Union/Select.ts b/sources/Union/Select.ts index 77f6feaa9..36f1c81b6 100644 --- a/sources/Union/Select.ts +++ b/sources/Union/Select.ts @@ -1,4 +1,3 @@ -import {Union} from './Union' import {Is} from '../Any/Is' import {Match} from '../Any/_Internal' @@ -11,9 +10,7 @@ import {Match} from '../Any/_Internal' * ```ts * ``` */ -export type Select = +export type Select = U extends unknown - ? Is extends 1 - ? U - : never + ? {1: U & M, 0: never}[Is] : never diff --git a/sources/Union/Strict.ts b/sources/Union/Strict.ts index f68d4da5d..61eb70984 100644 --- a/sources/Union/Strict.ts +++ b/sources/Union/Strict.ts @@ -1,5 +1,5 @@ import {ComputeRaw} from '../Any/Compute' -import {Keys} from './Keys' +import {Keys} from '../Any/Keys' import {OptionalFlat} from '../Object/Optional' import {Record} from '../Object/Record' diff --git a/sources/Union/Union.ts b/sources/Union/Union.ts deleted file mode 100644 index 478c56302..000000000 --- a/sources/Union/Union.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * A [[Union]] - * @example - * ```ts - * type union0 = 1 | 2 | 3 - * type union1 = 'a' | 420 - * ``` - */ -export type Union = any diff --git a/sources/Union/_api.ts b/sources/Union/_api.ts index 321624e8e..096c22ad4 100644 --- a/sources/Union/_api.ts +++ b/sources/Union/_api.ts @@ -6,7 +6,6 @@ export {Filter} from './Filter' export {Has} from './Has' export {Intersect} from './Intersect' export {IntersectOf} from './IntersectOf' -export {Keys} from './Keys' export {Last} from './Last' export {Merge} from './Merge' export {NonNullable} from './NonNullable' @@ -16,4 +15,3 @@ export {Replace} from './Replace' export {Select} from './Select' export {Strict} from './Strict' export {ListOf} from './ListOf' -export {Union} from './Union' diff --git a/sources/ts-toolbelt.ts b/sources/index.ts similarity index 100% rename from sources/ts-toolbelt.ts rename to sources/index.ts diff --git a/tests/Any.ts b/tests/Any.ts index 2a7c31f68..8c68e92d6 100644 --- a/tests/Any.ts +++ b/tests/Any.ts @@ -1,10 +1,49 @@ -import {Test, A} from '../sources/ts-toolbelt' +import {Test, A} from '../sources' const {checks, check} = Test // /////////////////////////////////////////////////////////////////////////////////////// // ANY /////////////////////////////////////////////////////////////////////////////////// +type O = { + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; + +type T = [ + 1, + 2, + '3' | undefined, + 'xxxx', + {a: 'a'} & {b: 'b'}, + string | number, + number, + object, + readonly [0, 1, 2?], + 'xxxx'? +]; + +// --------------------------------------------------------------------------------------- +// AT + +checks([ + check, string, Test.Pass>(), + check, {a: 'a'} & {b: 'b'}, Test.Pass>(), + check, O, Test.Pass>(), + check, 2, Test.Pass>(), + check, 'xxxx', Test.Pass>(), + check, 1 | undefined, Test.Pass>(), +]) + // --------------------------------------------------------------------------------------- // CAST @@ -96,6 +135,26 @@ checks([ // Cannot be tested +// --------------------------------------------------------------------------------------- +// KEYS + +checks([ + check, keyof O, Test.Pass>(), + check, 'a' | 'b', Test.Pass>(), + check<(keyof ({a: 0} | {b: 0})), 'a' & 'b', Test.Pass>(), +]) + +// --------------------------------------------------------------------------------------- +// KNOWNKEYS + +checks([ + check, Exclude, number>, Test.Pass>(), + check, never, Test.Pass>(), + check, keyof O, Test.Pass>(), + check, never, Test.Pass>(), + check, 'a', Test.Pass>(), +]) + // --------------------------------------------------------------------------------------- // IS diff --git a/tests/Boolean.ts b/tests/Boolean.ts index b39802bdb..fcc0b1997 100644 --- a/tests/Boolean.ts +++ b/tests/Boolean.ts @@ -1,4 +1,4 @@ -import {Test, B} from '../sources/ts-toolbelt' +import {Test, B} from '../sources' const {checks, check} = Test diff --git a/tests/Class.ts b/tests/Class.ts index 2a9e0c261..e7c2014e5 100644 --- a/tests/Class.ts +++ b/tests/Class.ts @@ -1,4 +1,4 @@ -import {Test, C} from '../sources/ts-toolbelt' +import {Test, C} from '../sources' const {checks, check} = Test diff --git a/tests/Community.ts b/tests/Community.ts index 8cfde42fb..d74a2d563 100644 --- a/tests/Community.ts +++ b/tests/Community.ts @@ -1,4 +1,4 @@ -import {Test, Community} from '../sources/ts-toolbelt' +import {Test, Community} from '../sources' const {checks, check} = Test @@ -6,41 +6,41 @@ const {checks, check} = Test // COMMUNITY ///////////////////////////////////////////////////////////////////////////// type O = { - a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O // recursion - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; // recursion + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; +}; type O1 = { - a : string | number - b : object - c : {a: 'a'} & {b: 'b'} - d?: never -readonly e?: 'string1' -readonly f : 0 - g : O1 // recursion - h : never - i : {a: string} - j : 'a' | undefined - k : {a: {b: string, c: 0}} + a: string | number; + b: object; + c: {a: 'a'} & {b: 'b'}; + d?: never; + readonly e?: 'string1'; + readonly f: 0; + g: O1; // recursion + h: never; + i: {a: string}; + j: 'a' | undefined; + k: {a: {b: string, c: 0}}; } // --------------------------------------------------------------------------------------- // INCLUDESDEEP type O_INCLUDESDEEP = { - a: { - b: { - x: number - } - } + a: { + b: { + x: number; + }; + }; } checks([ diff --git a/tests/Function.ts b/tests/Function.ts index 8d3898f28..14eeac2d5 100644 --- a/tests/Function.ts +++ b/tests/Function.ts @@ -1,4 +1,5 @@ -import {Test, F, A} from '../sources/ts-toolbelt' +import {Test, F, A} from '../sources' +import {Narrow} from '../sources/Function/Narrow' const {checks, check} = Test @@ -26,10 +27,16 @@ const composedSync = composeSync( // @ts-ignore (message: string) => false, // receive previous return (info: {name: string, age: number}) => `Welcome, ${info.name}`, // receive previous return - (generic: T) => generic, // receive previous return - (name: string, age: number) => ({name, age}), // receive parameters + (generic: T) => generic, // receive previous return + (name: string, age: number) => ({name, age}), // receive parameters ) +composeSync( + (c1: string[]) => [c1], + (b1: string) => [b1], + curry((a1: number, d2: number) => `${a1 + d2}`), +)(23, 42) + checks([ check<(typeof composedSync), (name: string, age: number) => boolean, Test.Pass>(), ]) @@ -41,11 +48,18 @@ declare const composeAsync: F.Compose<'async'> const composedAsync = composeAsync( // @ts-ignore (message: string) => false, // receive previous return + // @ts-ignore async (info: {name: string, age: number}) => `Welcome, ${info.name}`, // receive previous return async (generic: T) => generic, // receive previous return async (name: string, age: number) => ({name, age}), // receive parameters ) +composeAsync( + (c1: string[]) => [c1], + (b1: string) => [b1], + curry((a1: number, d2: number) => `${a1 + d2}`), +)(23, 42) + checks([ check<(typeof composedAsync), (name: string, age: number) => Promise, Test.Pass>(), ]) @@ -53,7 +67,7 @@ checks([ // --------------------------------------------------------------------------------------- // CURRY -declare function curry(f: Fn): F.Curry +declare function curry(f: Fn): F.Curry; const __ = {} as A.x @@ -67,6 +81,19 @@ const test02: boolean = curried(__, 26)(__, true, __)('Jane', 'JJ') // boolean const test03: boolean = curried('Jane', 26, true) // boolean const test04: boolean = curried('Jane', 26, true, 'JJ') // boolean +// --------------------------------------------------------------------------------------- +// EXACT + +declare function exactObject(x: F.Exact): A; + +const test07 = exactObject({} as {a: 1, b: 2}) +// @ts-expect-error +const test08 = exactObject({} as {a: 1}) + +checks([ + check(), +]) + // --------------------------------------------------------------------------------------- // PARAMETERS @@ -74,15 +101,132 @@ checks([ check, [string, number, object], Test.Pass>(), ]) +// --------------------------------------------------------------------------------------- +// AUTOPATH + +type O_AUTOPATH = { + a: { + a: 1; + }; + b: { + a: { + a: 2; + }; + b: O_AUTOPATH[]; + }; +}; + +checks([ + check, 'b/b/0/a' | 'b/b/0/a/a', Test.Pass>(), + check, 'a' | 'a.a', Test.Pass>(), + check, 'a.a', Test.Pass>(), + check, 'b.b' | 'b.a', Test.Pass>(), + check, 'b.b.0' | 'b.b.0.b' | 'b.b.0.a', Test.Pass>(), + check, 'b.b.0.a' | 'b.b.0.a.a', Test.Pass>(), + check, 'b.b.0.a' | 'b.b.x.a.a', Test.Fail>(), + check, 'b.b.0.a' | 'b.b.a.a', Test.Fail>(), + check, never, Test.Pass>(), +]) + +// --------------------------------------------------------------------------------------- +// VALIDPATH + +type O_VALIDPATH = { + a: { + a: {}; + }; + b: { + a: { + a: {}; + }; + b: string[]; + }; +}; + +checks([ + check, ['a', 'a'], Test.Pass>(), + check, ['a', 'a'], Test.Pass>(), + check, ['a', 'x'], Test.Pass>(), + check, ['b', 'a', 'a'], Test.Pass>(), + check, ['b', 'b', 0], Test.Pass>(), +]) + // --------------------------------------------------------------------------------------- // LENGTH checks([ check, 3, Test.Pass>(), check any>, 1 | 2, Test.Pass>(), - check any, 's'>, '1' | '2', Test.Pass>(), ]) +// --------------------------------------------------------------------------------------- +// NARROW + +declare function narrowList(x: F.Narrow): A; +declare function narrowObject(x: F.Narrow): A; + +const test05 = narrowList(['e', 2, true, {f: ['g', ['h']]}]) +const test06 = narrowObject({a: 1, b: 'c', d: ['e', 2, true, {f: ['g']}]}) + +checks([ + check(), + check(), +]) + +const narrow = (thing: Narrow) => thing + +interface Foo { + bar: {hi: 'there'} +} + +class Bar implements Foo { + bar = narrow({hi: 'there'}) +} + +type Co = () => Narrow; +function covariance(t: Narrow, u: Narrow, coT: Co, coU: Co) { + u = t + // @ts-expect-error + t = u + + coU = coT + // @ts-expect-error + coT = coU +} + +type Contra = (v: Narrow) => void; +function contravariance(t: Narrow, u: Narrow, contraT: Contra, contraU: Contra) { + u = t + // @ts-expect-error + t = u + + // @ts-expect-error + contraU = contraT + contraT = contraU +} + +type In = (v: Narrow) => Narrow; +function invariance(t: Narrow, u: Narrow, inT: In, inU: In) { + u = t + // @ts-expect-error + t = u + + // @ts-expect-error + inU = inT + // @ts-expect-error + inT = inU +} + +type Bi = { foo(v: Narrow): void }; +function bivariance(t: Narrow, u: Narrow, biT: Bi, biU: Bi) { + u = t + // @ts-expect-error + t = u + + biU = biT + biT = biU +} + // --------------------------------------------------------------------------------------- // PIPE @@ -92,12 +236,18 @@ declare const pipeSync: F.Pipe<'sync'> const pipedSync = pipeSync( (name: string, age: number) => ({name, age}), // receive parameters - (generic: T) => generic, // receive previous return - (info: {name: string, age: number}) => `Welcome, ${info.name}`, // receive previous return - // @ts-ignore - (message: string) => false, // receive previous return + (generic: T) => generic, // receive previous return + (info: {name: string, age: number}) => `Welcome, ${info.name}`, // receive previous return + // @ts-ignore + (message: string) => false, // receive previous return ) +pipeSync( + curry((a1: number, d2: number) => `${a1 + d2}`), + (b1: string) => [b1], + (c1: string[]) => [c1], +)(23, 42) + checks([ check<(typeof pipedSync), (name: string, age: number) => boolean, Test.Pass>(), ]) @@ -109,11 +259,18 @@ declare const pipeAsync: F.Pipe<'async'> const pipedAsync = pipeAsync( (name: string, age: number) => ({name, age}), // receive parameters async (generic: T) => generic, // receive previous return + // @ts-ignore async (info: {name: string, age: number}) => `Welcome, ${info.name}`, // receive previous return // @ts-ignore (message: string) => false, // receive previous return ) +pipeAsync( + curry((a1: number, d2: number) => `${a1 + d2}`), + (b1: string) => [b1], + (c1: string[]) => [c1], +)(23, 42) + checks([ check<(typeof pipedAsync), (name: string, age: number) => Promise, Test.Pass>(), ]) diff --git a/tests/Iteration.ts b/tests/Iteration.ts index 25c8782a7..102f37d4e 100644 --- a/tests/Iteration.ts +++ b/tests/Iteration.ts @@ -1,4 +1,4 @@ -import {Test, I} from '../sources/ts-toolbelt' +import {Test, I} from '../sources' const {checks, check} = Test diff --git a/tests/List.ts b/tests/List.ts index c0f5246b4..110e8119c 100644 --- a/tests/List.ts +++ b/tests/List.ts @@ -1,4 +1,4 @@ -import {Test, T, A} from '../sources/ts-toolbelt' +import {Test, T, A} from '../sources' const {checks, check} = Test @@ -6,31 +6,31 @@ const {checks, check} = Test // LIST ///////////////////////////////////////////////////////////////////////////////// type T = [ - 1, - 2, - '3' | undefined, - 'xxxx', - {a: 'a'} & {b: 'b'}, - string | number, - number, - object, - readonly [0, 1, 2?], - 'xxxx'? -] + 1, + 2, + '3' | undefined, + 'xxxx', + {a: 'a'} & {b: 'b'}, + string | number, + number, + object, + readonly [0, 1, 2?], + 'xxxx'? +]; type T1 = [ - 1, - 2, - '3', - 'xxxx', - string, - number, - number & {}, - object, - readonly [0, 1, 2?, 3?], - {a: never}, - 'xxxx'? -] + 1, + 2, + '3', + 'xxxx', + string, + number, + number & {}, + object, + readonly [0, 1, 2?, 3?], + {a: never}, + 'xxxx'? +]; // --------------------------------------------------------------------------------------- // APPEND @@ -47,35 +47,27 @@ checks([ check, [3, 2 | 1], Test.Pass>(), ]) -// --------------------------------------------------------------------------------------- -// AT - -checks([ - check, 2, Test.Pass>(), - check, 'xxxx', Test.Pass>(), -]) - // --------------------------------------------------------------------------------------- // ATLEAST type T_ATLEAST = [ - 0, - 1, - 2 + 0, + 1, + 2 ] | [ - 3, - 4, - 5, - 6 -] + 3, + 4, + 5, + 6 +]; type ATLEAST_T_013 = - | [0, 1, 2] - | [0, 1 | undefined, 2 | undefined] - | [0 | undefined, 1, 2 | undefined] - | [3, 4 | undefined, 5 | undefined, 6 | undefined] - | [3 | undefined, 4, 5 | undefined, 6 | undefined] - | [3 | undefined, 4 | undefined, 5 | undefined, 6] + | [0, 1, 2] + | [0, 1 | undefined, 2 | undefined] + | [0 | undefined, 1, 2 | undefined] + | [3, 4 | undefined, 5 | undefined, 6 | undefined] + | [3 | undefined, 4, 5 | undefined, 6 | undefined] + | [3 | undefined, 4 | undefined, 5 | undefined, 6]; checks([ check, ATLEAST_T_013, Test.Pass>(), @@ -122,12 +114,12 @@ checks([ // EITHER type T_EITHER = [ - 0, - 1, - 2 -] + 0, + 1, + 2 +]; -type EITHER_T_01 = [0, undefined, 2] | [undefined, 1, 2] +type EITHER_T_01 = [0, undefined, 2] | [undefined, 1, 2]; checks([ check, EITHER_T_01, Test.Pass>(), @@ -165,30 +157,30 @@ checks([ // FILTER type FILTER_T_NUMBER_EXTENDS = [ - '3' | undefined, - 'xxxx', - {a: 'a'} & {b: 'b'}, - string | number, - object, - readonly [ - 0, - 1, - 2?, - ], - 'xxxx' | undefined -] + '3' | undefined, + 'xxxx', + {a: 'a'} & {b: 'b'}, + string | number, + object, + readonly [ + 0, + 1, + 2?, + ], + 'xxxx' | undefined +]; type FILTER_T_NUMBER_EQUALS = [ - 1, - 2, - '3' | undefined, - 'xxxx', - {a: 'a'} & {b: 'b'}, - string | number, - object, - readonly [0, 1, 2?], - 'xxxx' | undefined -] + 1, + 2, + '3' | undefined, + 'xxxx', + {a: 'a'} & {b: 'b'}, + string | number, + object, + readonly [0, 1, 2?], + 'xxxx' | undefined +]; checks([ check, FILTER_T_NUMBER_EXTENDS, Test.Pass>(), @@ -204,8 +196,8 @@ checks([ // --------------------------------------------------------------------------------------- // FLATTEN -type T_FLATTEN = [1, 12, [2, [3, [4, [5, [6, [7, [8, [9, 92?]]]]]]]]] -type FLATTEN_T = [1, 12, 2, 3, 4, 5, 6, 7, 8, 9, 92] | [1, 12, 2, 3, 4, 5, 6, 7, 8, 9, undefined] +type T_FLATTEN = [1, 12, [2, [3, [4, [5, [6, [7, [8, [9, 92?]]]]]]]]]; +type FLATTEN_T = [1, 12, 2, 3, 4, 5, 6, 7, 8, 9, 92] | [1, 12, 2, 3, 4, 5, 6, 7, 8, 9, undefined]; checks([ check, any[], Test.Pass>(), @@ -219,11 +211,11 @@ checks([ // --------------------------------------------------------------------------------------- // GROUP -type T_GROUP = [1, 2, 3, 4, 5, 6, 7, 8] +type T_GROUP = [1, 2, 3, 4, 5, 6, 7, 8]; -type GROUP_T_1 = [[1], [2], [3], [4], [5], [6], [7], [8]] -type GROUP_T_2 = [[1, 2], [3, 4], [5, 6], [7, 8]] -type GROUP_T_3 = [[1, 2, 3], [4, 5, 6], [7, 8, undefined]] +type GROUP_T_1 = [[1], [2], [3], [4], [5], [6], [7], [8]]; +type GROUP_T_2 = [[1, 2], [3, 4], [5, 6], [7, 8]]; +type GROUP_T_3 = [[1, 2, 3], [4, 5, 6], [7, 8, undefined]]; checks([ check, GROUP_T_1, Test.Pass>(), @@ -247,7 +239,7 @@ checks([ checks([ check, 1, Test.Pass>(), - check, never, Test.Pass>(), + // check, never, Test.Pass>(), check, never, Test.Pass>(), ]) @@ -259,36 +251,38 @@ checks([ // --------------------------------------------------------------------------------------- // INTERSECT +type t = T.Intersect + type INTERSECT_T_T1_NUMBER_DEFAULT = [ - 1, - 2, - '3' | undefined, - 'xxxx', - {a: 'a'} & {b: 'b'}, - string | number, - number, - object, - readonly [0, 1, 2?], - 'xxxx' | undefined -] + 1, + 2, + '3' | undefined, + 'xxxx', + {a: 'a'} & {b: 'b'}, + string | number, + number, + object, + readonly [0, 1, 2?], + 'xxxx' | undefined +]; type INTERSECT_T_T1_NUMBER_EXTENDS = [ - 1, - 2, - '3' | undefined, - 'xxxx', - number | string, - number, - object, - readonly [0, 1, 2?] -] + 1, + 2, + '3' | undefined, + 'xxxx', + number | string, + number, + object, + readonly [0, 1, 2?] +]; type INTERSECT_T_T1_NUMBER_EQUALS = [ - 1, - 2, - 'xxxx', - object, -] + 1, + 2, + 'xxxx', + object, +]; checks([ check, INTERSECT_T_T1_NUMBER_DEFAULT, Test.Pass>(), @@ -301,15 +295,6 @@ checks([ // No test needed (same as O.IntersectKeys) -// --------------------------------------------------------------------------------------- -// KEYS - -type KEYS_T = number | '3' | '0' | '1' | '2' | '4' | '5' | '6' | '7' | '8' | '9' - -checks([ - check, KEYS_T, Test.Pass>(), -]) - // --------------------------------------------------------------------------------------- // KEYSET @@ -329,15 +314,14 @@ checks([ // LASTINDEX checks([ - check, 1 | 2, Test.Pass>(), + check, 1 | 2, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // LENGTH checks([ - check, 2 | 3, Test.Pass>(), - check, '2' | '3', Test.Pass>(), + check, 2 | 3, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- @@ -493,11 +477,6 @@ checks([ // No test needed (same as O.PathUp) -// --------------------------------------------------------------------------------------- -// PATHVALID - -// No test needed (same as O.PathValid) - // --------------------------------------------------------------------------------------- // PICK @@ -581,15 +560,15 @@ checks([ // SELECT type SELECT_T_NUMBER_EXTENDS = [ - 1, - 2, - string | number, - number -] + 1, + 2, + string | number, + number +]; type SELECT_T_NUMBER_EQUALS = [ - number -] + number +]; checks([ check, SELECT_T_NUMBER_EXTENDS, Test.Pass>(), @@ -659,7 +638,7 @@ checks([ // UNIONIZE checks([ - check, [2 | string], Test.Pass>(), + check, [2 | string | undefined], Test.Pass>(), check, Array, Test.Pass>(), check, [1 | 2], Test.Pass>(), ]) @@ -693,8 +672,8 @@ checks([ // --------------------------------------------------------------------------------------- // WRITABLE -type WRITABLE_W_T_ARR = ['a', 'b'] -type WRITABLE_R_T_ARR = readonly ['a', 'b'] +type WRITABLE_W_T_ARR = ['a', 'b']; +type WRITABLE_R_T_ARR = readonly ['a', 'b']; checks([ check, WRITABLE_W_T_ARR, Test.Pass>(), diff --git a/tests/Misc.ts b/tests/Misc.ts index ab92e50bc..8e8b8293a 100644 --- a/tests/Misc.ts +++ b/tests/Misc.ts @@ -1,4 +1,4 @@ -import {Test, M} from '../sources/ts-toolbelt' +import {Test, M} from '../sources' const {checks, check} = Test diff --git a/tests/Number.ts b/tests/Number.ts index 26a67c3ea..4adca979a 100644 --- a/tests/Number.ts +++ b/tests/Number.ts @@ -1,4 +1,4 @@ -import {Test, N} from '../sources/ts-toolbelt' +import {Test, N} from '../sources' const {checks, check} = Test @@ -8,7 +8,6 @@ const {checks, check} = Test // --------------------------------------------------------------------------------------- // ABSOLUTE - checks([ check, 0 | 1, Test.Pass>(), check, 0, Test.Pass>(), diff --git a/tests/Object.ts b/tests/Object.ts index 50bf7f677..b6ccfb165 100644 --- a/tests/Object.ts +++ b/tests/Object.ts @@ -1,4 +1,4 @@ -import {Test, O, A, T, U, M} from '../sources/ts-toolbelt' +import {Test, O, A, T, U, M, F, S} from '../sources' import {Key} from '../sources/Any/Key' import {OptionalDeep} from '../sources/Object/Optional' @@ -8,97 +8,87 @@ const {checks, check} = Test // OBJECT //////////////////////////////////////////////////////////////////////////////// type O = { - a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; type O1 = { - a : string | number - b : object - c : {a: 'a'} & {b: 'b'} - d?: never -readonly e?: 'string1' -readonly f : 0 - g : {} - h : never - i : {a: string} - j : 'a' | undefined - k : {a: {b: string, c: 0}} - l : [1, 2, 3] -} + a: string | number; + b: object; + c: {a: 'a'} & {b: 'b'}; + d?: never; + readonly e?: 'string1'; + readonly f: 0; + g: {}; + h: never; + i: {a: string}; + j: 'a' | undefined; + k: {a: {b: string, c: 0}}; + l: [1, 2, 3]; +}; // --------------------------------------------------------------------------------------- // ASSIGN -type O_ASSIGN = {readonly a: 1, c: 2} -type Os_ASSIGN = [{a: 2, readonly b: 1}, {a: 3, c?: 1}] +type O_ASSIGN = {readonly a: 1, c: 2}; +type Os_ASSIGN = [{a: 2, readonly b: 1}, {a: 3, c?: 1}]; -type ASSIGN_O_Os = {readonly a: 3, readonly b: 1, c: 1 | 2} +type ASSIGN_O_Os = {readonly a: 3, readonly b: 1, c: 1 | 2}; checks([ check, ASSIGN_O_Os, Test.Pass>(), ]) -// --------------------------------------------------------------------------------------- -// AT - -checks([ - check, string, Test.Pass>(), - check, {a: 'a'} & {b: 'b'}, Test.Pass>(), - check, O, Test.Pass>(), - check, 1 | 2, Test.Pass>(), -]) - // --------------------------------------------------------------------------------------- // ATLEAST type O_ATLEAST = { - a?: 1 - b?: 2 - c?: 3 - d: 4 + a?: 1; + b?: 2; + c?: 3; + d: 4; } | { - e: 5 - f: 6 + e: 5; + f: 6; } | { - g?: 7 - h?: 8 -} + g?: 7; + h?: 8; +}; type ATLEAST_O_ABF = { - a: 1 - b: 2 - c: 3 - d: 4 + a: 1; + b: 2; + c: 3; + d: 4; } | { - a: 1 - b?: 2 - c?: 3 - d?: 4 + a: 1; + b?: 2; + c?: 3; + d?: 4; } | { - a?: 1 - b: 2 - c?: 3 - d?: 4 + a?: 1; + b: 2; + c?: 3; + d?: 4; } | { - e: 5 - f: 6 + e: 5; + f: 6; } | { - e?: 5 - f: 6 + e?: 5; + f: 6; } | { - g?: 7 - h?: 8 -} + g?: 7; + h?: 8; +}; checks([ check, ATLEAST_O_ABF, Test.Pass>(), @@ -108,18 +98,18 @@ checks([ // COMPULSORY type COMPULSORY_O = { - a: string, - b: number - c: {a: 'a'} & {b: 'b'} - d: 'string0' -readonly e: 'string1' -readonly f: 0 - g: O // recursion - h: 1 - j: 'a' - k: {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d: 'string0'; + readonly e: 'string1'; + readonly f: 0; + g: O; // recursion + h: 1; + j: 'a'; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, COMPULSORY_O, Test.Pass>(), @@ -135,7 +125,7 @@ function COMPULSORY_GENERIC(o: O) { // --------------------------------------------------------------------------------------- // COMPULSORYKEYS -type COMPULSORYKEYS_O = 'a' | 'b' | 'c' | 'f' | 'g' | 'k' | 'x' +type COMPULSORYKEYS_O = 'a' | 'b' | 'c' | 'f' | 'g' | 'k' | 'x'; checks([ check, COMPULSORYKEYS_O, Test.Pass>(), @@ -145,22 +135,22 @@ checks([ // DIFF type DIFF_O_O1_DEFAULT = { - i: {a: string} - l: [1, 2, 3] - x: () => 1 -} + i: {a: string}; + l: [1, 2, 3]; + x: () => 1; +}; type DIFF_O_O1_EQUALS = { - a : string - b : number - d?: 'string0' - g : O - h?: 1 - i : {a: string} - k : {a: {b: string}} - l : [1, 2, 3] - x: () => 1 -} + a: string; + b: number; + d?: 'string0'; + g: O; + h?: 1; + i: {a: string}; + k: {a: {b: string}}; + l: [1, 2, 3]; + x: () => 1; +}; checks([ check, DIFF_O_O1_DEFAULT, Test.Pass>(), @@ -170,22 +160,22 @@ checks([ // --------------------------------------------------------------------------------------- type DIFF_O1_O_DEFAULT = { - i: {a: string} - l: [1, 2, 3] - x: () => 1 -} + i: {a: string}; + l: [1, 2, 3]; + x: () => 1; +}; type DIFF_O1_O_EQUALS = { - a : string | number - b : object - d?: never - g : {} - h : never - i : {a: string} - k : {a: {b: string, c: 0}} - l : [1, 2, 3] - x: () => 1 -} + a: string | number; + b: object; + d?: never; + g: {}; + h: never; + i: {a: string}; + k: {a: {b: string, c: 0}}; + l: [1, 2, 3]; + x: () => 1; +}; checks([ check, DIFF_O1_O_DEFAULT, Test.Pass>(), @@ -196,41 +186,41 @@ checks([ // EITHER type O_EITHER = { - a : string - b?: number -readonly c?: object + a: string; + b?: number; + readonly c?: object; } | { - a: 'a', - b: 'b' -} + a: 'a', + b: 'b'; +}; type EITHER_O_AB_TRUE = { - a: string - b?: undefined - readonly c?: object + a: string; + b?: undefined; + readonly c?: object; } | { - a?: undefined - b?: number - readonly c?: object + a?: undefined; + b?: number; + readonly c?: object; } | { - a: 'a' - b?: undefined + a: 'a'; + b?: undefined; } | { - b: 'b' - a?: undefined -} + b: 'b'; + a?: undefined; +}; type EITHER_O_AB_FALSE = { - a: string - readonly c?: object + a: string; + readonly c?: object; } | { - b?: number - readonly c?: object + b?: number; + readonly c?: object; } | { - a: 'a' + a: 'a'; } | { - b: 'b' -} + b: 'b'; +}; checks([ check, EITHER_O_AB_TRUE, Test.Pass>(), @@ -241,18 +231,18 @@ checks([ // EXCLUDE type EXCLUDE_O_O1_DEFAULT = { - x: () => 1 -} + x: () => 1; +}; type EXCLUDE_O_O1_EQUALS = { - a : string - b : number - d?: 'string0' - g : O - h?: 1 - k : {a: {b: string}} - x: () => 1 -} + a: string; + b: number; + d?: 'string0'; + g: O; + h?: 1; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, EXCLUDE_O_O1_DEFAULT, Test.Pass>(), @@ -262,20 +252,20 @@ checks([ // --------------------------------------------------------------------------------------- type EXCLUDE_O1_O_DEFAULT = { - i: {a: string} - l: [1, 2, 3] -} + i: {a: string}; + l: [1, 2, 3]; +}; type EXCLUDE_O1_O_EQUALS = { - a : string | number - b : object - d?: never - g : {} - h : never - i : {a: string} - k : {a: {b: string, c: 0}} - l: [1, 2, 3] -} + a: string | number; + b: object; + d?: never; + g: {}; + h: never; + i: {a: string}; + k: {a: {b: string, c: 0}}; + l: [1, 2, 3]; +}; checks([ check, EXCLUDE_O1_O_DEFAULT, Test.Pass>(), @@ -285,9 +275,9 @@ checks([ // --------------------------------------------------------------------------------------- // EXCLUDEKEYS -type EXCLUDEKEYS_O_DEFAULT = 'x' +type EXCLUDEKEYS_O_DEFAULT = 'x'; -type EXCLUDEKEYS_O_EQUALS = 'a' | 'b' | 'd' | 'g' | 'h' | 'k' | 'x' +type EXCLUDEKEYS_O_EQUALS = 'a' | 'b' | 'd' | 'g' | 'h' | 'k' | 'x'; checks([ check, EXCLUDEKEYS_O_DEFAULT, Test.Pass>(), @@ -296,9 +286,9 @@ checks([ // --------------------------------------------------------------------------------------- -type EXCLUDEKEYS_O1_DEFAULT = 'i' | 'l' +type EXCLUDEKEYS_O1_DEFAULT = 'i' | 'l'; -type EXCLUDEKEYS_O1_EQUALS = 'a' | 'b' | 'd' | 'g' | 'h' | 'i' | 'k' | 'l' +type EXCLUDEKEYS_O1_EQUALS = 'a' | 'b' | 'd' | 'g' | 'h' | 'i' | 'k' | 'l'; checks([ check, EXCLUDEKEYS_O1_DEFAULT, Test.Pass>(), @@ -309,30 +299,30 @@ checks([ // FILTER type FILTER_O_DEFAULT = { - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; type FILTER_O_EQUALS = { - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check'>, FILTER_O_DEFAULT, Test.Pass>(), @@ -342,9 +332,9 @@ checks([ // --------------------------------------------------------------------------------------- // FILTERKEYS -type FILTERKEYS_O_DEFAULT = 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'j' | 'k' | 'x' +type FILTERKEYS_O_DEFAULT = 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'j' | 'k' | 'x'; -type FILTERKEYS_O_EQUALS = 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'j' | 'k' | 'x' +type FILTERKEYS_O_EQUALS = 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'j' | 'k' | 'x'; checks([ check'>, FILTERKEYS_O_DEFAULT, Test.Pass>(), @@ -406,14 +396,14 @@ checks([ // --------------------------------------------------------------------------------------- // INTERSECT -type INTERSECT_O_O1_DEFAULT = O.Omit +type INTERSECT_O_O1_DEFAULT = O.Omit; type INTERSECT_O_O1_EQUALS = { - c : {a: 'a'} & {b: 'b'} -readonly e?: 'string1' -readonly f : 0 - j : 'a' | undefined -} + c: {a: 'a'} & {b: 'b'}; + readonly e?: 'string1'; + readonly f: 0; + j: 'a' | undefined; +}; checks([ check, INTERSECT_O_O1_DEFAULT, Test.Pass>(), @@ -423,9 +413,9 @@ checks([ // --------------------------------------------------------------------------------------- // INTERSECTKEYS -type INTERSECTKEYS_O_DEFAULT = U.Exclude +type INTERSECTKEYS_O_DEFAULT = U.Exclude; -type INTERSECTKEYS_O_EQUALS = 'c' | 'e' | 'f' | 'j' +type INTERSECTKEYS_O_EQUALS = 'c' | 'e' | 'f' | 'j'; checks([ check, INTERSECTKEYS_O_DEFAULT, Test.Pass>(), @@ -436,58 +426,96 @@ checks([ // INVERT const INVERT_SYM = Symbol('') +const INVERT_SYM2 = Symbol('') + +type O_INVERT_T1 = { + A: 'Av'; + B: typeof INVERT_SYM; + C: 42; +}; + +type O_INVERT_T2 = O_INVERT_T1 | { + Af: 'Avf'; + Bf: typeof INVERT_SYM2; + Cf: 43; +}; + +type T1_INVERT_O = { + Av: 'A'; + [INVERT_SYM]: 'B'; + 42: 'C'; +}; + +type T2_INVERT_O = { + Av: 'A'; + [INVERT_SYM]: 'B'; + 42: 'C'; +} | { + Avf: 'Af'; + [INVERT_SYM2]: 'Bf'; + 43: 'Cf'; +}; -type O_INVERT = { - A: 'Av', - B: typeof INVERT_SYM, - C: 42 +interface O_INVERT_I1 { + A: 'Av'; + B: typeof INVERT_SYM; + C: 42; } -type INVERT_O = { - Av : 'A' - [INVERT_SYM]: 'B' - 42 : 'C' +interface O_INVERT_I2 { + Af: 'Avf'; + Bf: typeof INVERT_SYM2; + Cf: 43; } -checks([ - check, INVERT_O, Test.Pass>(), -]) +interface I1_INVERT_O { + Av: 'A'; + [INVERT_SYM]: 'B'; + 42: 'C'; +} -// --------------------------------------------------------------------------------------- -// KEYS +interface I2_INVERT_O { + Avf: 'Af'; + [INVERT_SYM2]: 'Bf'; + 43: 'Cf'; +} checks([ - check, keyof O, Test.Pass>(), - check, keyof O1, Test.Pass>(), + check, T1_INVERT_O, Test.Pass>(), + check, T2_INVERT_O, Test.Pass>(), + check, T1_INVERT_O, Test.Pass>(), + check, I1_INVERT_O, Test.Pass>(), + check, T2_INVERT_O, Test.Pass>(), + check, I1_INVERT_O | I2_INVERT_O, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // LISTOF type O_LISTOF_INDEX = { - '0': 1 - '2': 3 - '3': never - '5': 5 - '6': 6 -} + '0': 1; + '2': 3; + '3': never; + '5': 5; + '6': 6; +}; type O_LISTOF_NUMBER = { - [K in number]: 42 -} + [K in number]: 42 +}; type O_LISTOF_STRING = { - [K in string]: 42 -} + [K in string]: 42 +}; type O_LISTOF_SYMBOL = { - [K in symbol]: 42 -} + [K in symbol]: 42 +}; -type LISTOF_INDEX_O = [1, 3, never, 5, 6] -type LISTOF_NUMBER_O = 42[] -type LISTOF_STRING_O = 42[] -type LISTOF_SYMBOL_O = unknown[] +type LISTOF_INDEX_O = [1, 3, never, 5, 6]; +type LISTOF_NUMBER_O = 42[]; +type LISTOF_STRING_O = 42[]; +type LISTOF_SYMBOL_O = unknown[]; checks([ check, LISTOF_INDEX_O, Test.Pass>(), @@ -500,173 +528,173 @@ checks([ // MERGE type O_MERGE = { - a?: string - c: { - a?: string - b?: number - } | Date - d: 'hello' | undefined - e: number | {a: 1} - f?: { - a : string - b?: number - }, - g?: { - a?: string - b?: number - } - h: { - a: number - b: number - } | undefined - i: { - a: string - } | undefined - j: { - a: { - b?: {} - } - }, - k?: {[k: string]: string} - l: [{a: 'a'}] - n: 42 -} + a?: string; + c: { + a?: string; + b?: number; + } | Date; + d: 'hello' | undefined; + e: number | {a: 1}; + f?: { + a: string; + b?: number; + }, + g?: { + a?: string; + b?: number; + }; + h: { + a: number; + b: number; + } | undefined; + i: { + a: string; + } | undefined; + j: { + a: { + b?: {}; + }; + }, + k?: {[k: string]: string}; + l: [{a: 'a'}]; + n: 42; +}; type O1_MERGE = { - a: object | undefined - b: number - c: { - a : object - b?: object - c : object - } - d: 'goodbye' - e: string | {b: 2} - f?: { - a : object - b?: object - c : object - } - h: { - a: string - } - i: { - a: number - } | undefined - j?: { - a: { - b?: { - c: 1 - } - } - } - k: {} | Date - l: [{b: 'b'}, 2, 3] - m: [] -} + a: object | undefined; + b: number; + c: { + a: object; + b?: object; + c: object; + }; + d: 'goodbye'; + e: string | {b: 2}; + f?: { + a: object; + b?: object; + c: object; + }; + h: { + a: string; + }; + i: { + a: number; + } | undefined; + j?: { + a: { + b?: { + c: 1; + }; + }; + }; + k: {} | Date; + l: [{b: 'b'}, 2, 3]; + m: []; +}; type MERGE_O_O1_LODASH = { - a : string | object | undefined - b : number - c : { - a?: string - b?: number - } | Date - d : 'hello' | 'goodbye' - e : number | {a: 1} - f?: { - a : string - b?: number - } | { - a : object - b?: object - c : object - }, - g?: { - a?: string - b?: number - } - h: { - a: number - b: number - } | { - a: string - } - i: { - a: string - } | { - a: number - } | undefined - j: { - a: { - b?: {} - } - }, - k: {} | {[k: string]: string} | Date - l: [{a: 'a'}] - m: [] - n: 42 -} + a: string | object | undefined; + b: number; + c: { + a?: string; + b?: number; + } | Date; + d: 'hello' | 'goodbye'; + e: number | {a: 1}; + f?: { + a: string; + b?: number; + } | { + a: object; + b?: object; + c: object; + }, + g?: { + a?: string; + b?: number; + }; + h: { + a: number; + b: number; + } | { + a: string; + }; + i: { + a: string; + } | { + a: number; + } | undefined; + j: { + a: { + b?: {}; + }; + }, + k: {} | {[k: string]: string} | Date; + l: [{a: 'a'}]; + m: []; + n: 42; +}; type MERGE_O_O1_DEEP_LODASH = { - a : string | object | undefined - b : number - c : { - a : string | object - b?: number | object - c : object - } | Date - d : 'hello' | 'goodbye' - e : number | {a: 1} | {a: 1, b: 2} - f?: { - a: string - b?: number | undefined - } | { - a: object - b?: object | undefined - c: object - } | { - a: string - b?: number | object | undefined - c: object - } | undefined - g?: { - a?: string - b?: number - } - h: { - a: number - b: number - } | { - a: string - } - i: { - a: string - } | { - a: number - } | undefined - j: { - a: { - b?: {} | { - c: 1 - } - } - } | { - a: { - b?: {} - } - }, - k: {} | {[k: string]: string} | {[x: string]: string} | Date - l: [{a: 'a', b: 'b'}, 2, 3] - m: [] - n: 42 -} + a: string | object | undefined; + b: number; + c: { + a: string | object; + b?: number | object; + c: object; + } | Date; + d: 'hello' | 'goodbye'; + e: number | {a: 1} | {a: 1, b: 2}; + f?: { + a: string; + b?: number | undefined; + } | { + a: object; + b?: object | undefined; + c: object; + } | { + a: string; + b?: number | object | undefined; + c: object; + } | undefined; + g?: { + a?: string; + b?: number; + }; + h: { + a: number; + b: number; + } | { + a: string; + }; + i: { + a: string; + } | { + a: number; + } | undefined; + j: { + a: { + b?: {} | { + c: 1; + }; + }; + } | { + a: { + b?: {}; + }; + }, + k: {} | {[k: string]: string} | {[x: string]: string | undefined} | Date; + l: [{a: 'a', b: 'b'}, 2, 3]; + m: []; + n: 42; +}; checks([ check, [1, 3], Test.Pass>(), check, [1, 3], Test.Pass>(), - check, MERGE_O_O1_LODASH, Test.Pass>(), - check, MERGE_O_O1_DEEP_LODASH, Test.Pass>(), + check, MERGE_O_O1_LODASH, Test.Pass>(), + check, MERGE_O_O1_DEEP_LODASH, Test.Pass>(), ]) function MERGE_GENERIC(o: O) { @@ -680,8 +708,8 @@ function MERGE_GENERIC(o: O) { // MERGEALL checks([ - check, MERGE_O_O1_LODASH, Test.Pass>(), - check, MERGE_O_O1_DEEP_LODASH, Test.Pass>(), + check, MERGE_O_O1_LODASH, Test.Pass>(), + check, MERGE_O_O1_DEEP_LODASH, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- @@ -696,32 +724,32 @@ checks([ // NONNULLABLE type NONNULLABLE_O_FLAT = { - a : string - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' - k : {a: {b: string}} - x: () => 1 -} + a: string; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a'; + k: {a: {b: string}}; + x: () => 1; +}; type NONNULLABLE_O_J_FLAT = { - a : string - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' - k : {a: {b: string}} - x: () => 1 -} + a: string; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a'; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, NONNULLABLE_O_FLAT, Test.Pass>(), @@ -747,32 +775,32 @@ checks([ // NULLABLE type NULLABLE_O_FLAT = { - a : string | undefined | null - b : number | undefined | null - c : {a: 'a'} & {b: 'b'} | undefined | null - d?: 'string0' | null -readonly e?: 'string1' | null -readonly f : 0 | undefined | null - g : O | undefined | null - h?: 1 | null - j : 'a' | undefined | null - k : {a: {b: string}} | undefined | null - x: (() => 1) | undefined | null -} + a: string | undefined | null; + b: number | undefined | null; + c: {a: 'a'} & {b: 'b'} | undefined | null; + d?: 'string0' | null; + readonly e?: 'string1' | null; + readonly f: 0 | undefined | null; + g: O | undefined | null; + h?: 1 | null; + j: 'a' | undefined | null; + k: {a: {b: string}} | undefined | null; + x: (() => 1) | undefined | null; +}; type NULLABLE_O_A_FLAT = { - a : string | undefined | null - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string | undefined | null; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, NULLABLE_O_FLAT, Test.Pass>(), @@ -803,15 +831,15 @@ checks([ // OMIT type OMIT_O_DEH = { - a: string - b: number - c: {a: 'a'} & {b: 'b'} - readonly f: 0 - g: O - j: 'a' | undefined - k: {a: {b: string}} - x: () => 1 -} + a: string; + b: number; + c: {a: 'a'} & {b: 'b'}; + readonly f: 0; + g: O; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, OMIT_O_DEH, Test.Pass>(), @@ -822,32 +850,32 @@ checks([ // OPTIONAL type OPTIONAL_O_FLAT = { - a?: string, - b?: number - c?: {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f?: 0 - g?: O - h?: 1 - j?: 'a' - k?: {a: {b: string}} - x?: () => 1 -} + a?: string, + b?: number; + c?: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f?: 0; + g?: O; + h?: 1; + j?: 'a'; + k?: {a: {b: string}}; + x?: () => 1; +}; type OPTIONAL_O_A_FLAT = { - a?: string - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x : () => 1 -} + a?: string; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, OPTIONAL_O_FLAT, Test.Pass>(), @@ -870,18 +898,18 @@ function OPTIONAL_GENERIC(o: O) { // PARTIAL type PARTIAL_O_FLAT = { - a?: string, - b?: number - c?: {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f?: 0 - g?: O - h?: 1 - j?: 'a' - k?: {a: {b: string}} - x?: () => 1 -} + a?: string, + b?: number; + c?: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f?: 0; + g?: O; + h?: 1; + j?: 'a'; + k?: {a: {b: string}}; + x?: () => 1; +}; checks([ check, PARTIAL_O_FLAT, Test.Pass>(), @@ -906,52 +934,52 @@ checks([ // PATCH type PATCH_O_O1 = { - a : string - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - i : {a: string} - j : 'a' | undefined - k : {a: {b: string}} - l : [1, 2, 3] - x : () => 1 -} + a: string; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + i: {a: string}; + j: 'a' | undefined; + k: {a: {b: string}}; + l: [1, 2, 3]; + x: () => 1; +}; type PATCH_O1_O = { - a : string | number - b : object - c : {a: 'a'} & {b: 'b'} - d?: never -readonly e?: 'string1' -readonly f : 0 - g : {} - h : never - i : {a: string} - j : 'a' | undefined - k : {a: {b: string, c: 0}} - l : [1, 2, 3] - x : () => 1 -} + a: string | number; + b: object; + c: {a: 'a'} & {b: 'b'}; + d?: never; + readonly e?: 'string1'; + readonly f: 0; + g: {}; + h: never; + i: {a: string}; + j: 'a' | undefined; + k: {a: {b: string, c: 0}}; + l: [1, 2, 3]; + x: () => 1; +}; type PATCH_O_O1_DEEP = { - a : string - b : number - c : {a: 'a', b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - i : {a: string} - j : 'a' | undefined - k : {a: {b: string, c: 0}} - l : [1, 2, 3] - x : () => 1 -} + a: string; + b: number; + c: {a: 'a', b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + i: {a: string}; + j: 'a' | undefined; + k: {a: {b: string, c: 0}}; + l: [1, 2, 3]; + x: () => 1; +}; checks([ check, PATCH_O_O1, Test.Pass>(), @@ -982,86 +1010,67 @@ checks([ checks([ check, O['g'], Test.Pass>(), check, string, Test.Pass>(), - check, never, Test.Pass>(), - check, O, Test.Pass>(), + check, undefined, Test.Pass>(), + // check, undefined, Test.Pass>(), check, 'string0' | undefined, Test.Pass>(), ]) type O_PATH_U = { - b: { - c: { - d: 'bcd' - } - b: 'bb' - } + b: { + c: { + d: 'bcd'; + }; + b: 'bb'; + }; + c: 1; } | { - a: { - b: boolean | { - c: 'abc' - } - } -} + a: { + b: boolean | { + c: 'abc'; + }; + }; + c: 2; +}; checks([ - check, never, Test.Pass>(), - check, 'bcd', Test.Pass>(), - check, 'abc', Test.Pass>(), - check, boolean | 'bb' | {c: 'abc'}, Test.Pass>(), + check, 1 | 2, Test.Pass>(), + check, undefined, Test.Pass>(), + check, 'bcd' | undefined, Test.Pass>(), + check, 'abc' | undefined, Test.Pass>(), + check, boolean | 'bb' | {c: 'abc'} | undefined, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // PATHS type O_PATHS = { - a: { - a: boolean - } - b: { - a: { - a: {} - } - b: {} - } -} - -type Index = (string | number | symbol) - -checks([ - check, Index[], Test.Pass>(), - check, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?]>, Test.Pass>(), -]) - -// --------------------------------------------------------------------------------------- -// PATHVALID - -type O_PATHVALID = { - a: { - a: {} - } - b: { - a: { - a: {} - } - b: string[] - } -} + a: { + a: boolean; + }; + b: { + a: { + a: {}; + }; + b: {}; + }; +} | { + c: boolean +}; checks([ - check, ['a', 'a'], Test.Pass>(), - check, ['a', 'a'], Test.Pass>(), - check, ['a', 'a'], Test.Pass>(), - check, ['b', 'a', 'a'], Test.Pass>(), - check, ['b', 'b', 0], Test.Pass>(), + check, T.NonNullable<['prop'?, number?, 'a'?]>, Test.Pass>(), + check, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?] | ['c'?]>, Test.Pass>(), + check, O.Paths, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // PICK type PICK_O_DEF = { - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 -} + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; +}; checks([ check, PICK_O_DEF, Test.Pass>(), @@ -1072,32 +1081,32 @@ checks([ // READONLY type READONLY_O_FLAT = { -readonly a : string, -readonly b : number -readonly c : {a: 'a'} & {b: 'b'} -readonly d?: 'string0' -readonly e?: 'string1' -readonly f : 0 -readonly g : O -readonly h?: 1 -readonly j : 'a' | undefined -readonly k : {a: {b: string}} -readonly x : () => 1 -} + readonly a: string, + readonly b: number; + readonly c: {a: 'a'} & {b: 'b'}; + readonly d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + readonly g: O; + readonly h?: 1; + readonly j: 'a' | undefined; + readonly k: {a: {b: string}}; + readonly x: () => 1; +}; type READONLY_O_A_FLAT = { -readonly a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + readonly a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, READONLY_O_FLAT, Test.Pass>(), @@ -1128,24 +1137,24 @@ checks([ // RECORD type RECORD_AB_A_OPTR = { - readonly a?: string - readonly b?: string -} + readonly a?: string; + readonly b?: string; +}; type RECORD_AB_A_OPTW = { - a?: string - b?: string -} + a?: string; + b?: string; +}; type RECORD_AB_A_REQR = { - readonly a: string - readonly b: string -} + readonly a: string; + readonly b: string; +}; type RECORD_AB_A_REQW = { - a: string - b: string -} + a: string; + b: string; +}; checks([ check, RECORD_AB_A_OPTR, Test.Pass>(), @@ -1158,22 +1167,22 @@ checks([ // REPLACE type REPLACE_STRING_NUMBER = { - a: number - b: number - c: {a: 'a'} & {b: 'b'} - d?: 'string0' - readonly e?: 'string1' - readonly f: 0 - g: O - h?: 1 - j: 'a' | undefined - k: { - a: { - b: string - } - } - x: () => 1 -} + a: number; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: { + a: { + b: string; + }; + }; + x: () => 1; +}; checks([ check, REPLACE_STRING_NUMBER, Test.Pass>(), @@ -1183,32 +1192,32 @@ checks([ // REQUIRED type REQUIRED_O_FLAT = { - a: string, - b: number - c: {a: 'a'} & {b: 'b'} - d: 'string0' -readonly e: 'string1' -readonly f: 0 - g: O - h: 1 - j: 'a' | undefined - k: {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d: 'string0'; + readonly e: 'string1'; + readonly f: 0; + g: O; + h: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; type REQUIRED_O_D_FLAT = { - a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d : 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, REQUIRED_O_FLAT, Test.Pass>(), @@ -1234,15 +1243,15 @@ checks([ // SELECT type SELECT_O_DEFAULT = { - a: string - d?: 'string0' -readonly e?: 'string1' - j: 'a' | undefined -} + a: string; + d?: 'string0'; + readonly e?: 'string1'; + j: 'a' | undefined; +}; type SELECT_O_EQUALS = { - a : string, -} + a: string, +}; checks([ check'>, SELECT_O_DEFAULT, Test.Pass>(), @@ -1252,9 +1261,9 @@ checks([ // --------------------------------------------------------------------------------------- // SELECTKEYS -type SELECTKEYS_O_DEFAULT = 'a' | 'd' | 'e' | 'j' +type SELECTKEYS_O_DEFAULT = 'a' | 'd' | 'e' | 'j'; -type SELECTKEYS_O_EQUALS = 'a' +type SELECTKEYS_O_EQUALS = 'a'; checks([ check'>, SELECTKEYS_O_DEFAULT, Test.Pass>(), @@ -1265,32 +1274,32 @@ checks([ // UNDEFINABLE type UNDEFINABLE_O_FLAT = { - a : string | undefined - b : number | undefined - c : {a: 'a'} & {b: 'b'} | undefined - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 | undefined - g : O | undefined - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} | undefined - x: (() => 1) | undefined -} + a: string | undefined; + b: number | undefined; + c: {a: 'a'} & {b: 'b'} | undefined; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0 | undefined; + g: O | undefined; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}} | undefined; + x: (() => 1) | undefined; +}; type UNDEFINABLE_O_A_FLAT = { - a : string | undefined - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' -readonly e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string | undefined; + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + readonly e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, UNDEFINABLE_O_FLAT, Test.Pass>(), @@ -1316,25 +1325,25 @@ checks([ // UNIONIZE type O_UNIONIZE = { - a : 'a' - b : 'b' - c : never - d?: 1 -} + a: 'a'; + b: 'b'; + c: never; + d?: 1; +}; type O1_UNIONIZE = { - a : 'b' - b?: 'x' - c : 42 - d : {} -} + a: 'b'; + b?: 'x'; + c: 42; + d: {}; +}; type UNIONIZE_O_O1 = { - a : 'a' | 'b' - b : 'b' | 'x' | undefined - c : 42 - d?: {} | 1 -} + a: 'a' | 'b'; + b: 'b' | 'x' | undefined; + c: 42; + d?: {} | 1; +}; checks([ check, UNIONIZE_O_O1, Test.Pass>(), @@ -1344,13 +1353,13 @@ checks([ // UNIONOF type O_UNIONOF = { - a: 'a' - b: 'b' - c: never - d: 1 -} + a: 'a'; + b: 'b'; + c: never; + d: 1; +}; -type UNIONOF_O = 'a' | 'b' | 1 +type UNIONOF_O = 'a' | 'b' | 1; checks([ check, UNIONOF_O, Test.Pass>(), @@ -1360,22 +1369,22 @@ checks([ // UPDATE type O_UPDATE = { - a?: 'a' -} + a?: 'a'; +}; type UPDATE_O = { - a?: 'xxxx' -} + a?: 'xxxx'; +}; type UPDATE_O_X = { - a?: 'a' -} + a?: 'a'; +}; type UPDATE_O_STRING_42 = { - [x: string]: 42 - // @ts-ignore - a?: 42 | undefined -} + [x: string]: 42; + // @ts-ignore + a?: 42 | undefined; +}; checks([ check, UPDATE_O, Test.Pass>(), @@ -1386,32 +1395,32 @@ checks([ // WRITABLE type WRITABLE_O_FLAT = { - a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' - e?: 'string1' - f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + e?: 'string1'; + f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; type WRITABLE_O_E_FLAT = { - a : string, - b : number - c : {a: 'a'} & {b: 'b'} - d?: 'string0' - e?: 'string1' -readonly f : 0 - g : O - h?: 1 - j : 'a' | undefined - k : {a: {b: string}} - x: () => 1 -} + a: string, + b: number; + c: {a: 'a'} & {b: 'b'}; + d?: 'string0'; + e?: 'string1'; + readonly f: 0; + g: O; + h?: 1; + j: 'a' | undefined; + k: {a: {b: string}}; + x: () => 1; +}; checks([ check, WRITABLE_O_FLAT, Test.Pass>(), @@ -1439,480 +1448,355 @@ checks([ // /////////////////////////////////////////////////////////////////////////////////////// // OBJECT.P ////////////////////////////////////////////////////////////////////////////// -type OP = { // A binary tree - a: { - a: string - b: { - a: 'aba' - b: 'abb' - } - } - b?: { - a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - } - } - c?: string -} +type OP = {// A binary tree + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + }; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + }; + }; + c?: string; +}; type OP_UNIONS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - } - } | 'a' - b?: { - a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - } - } | 'b' - c?: string -} + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + } | 'a'; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + }; + } | 'b'; + c?: string; +}; type OP_ARRAYS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - }[] - }[][] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - }[] - }[] - c?: string -} + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }[]; + }[][]; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }[]; + b: { + a: 'bba'; + b: 'bbb'; + }[]; + }[]; + c?: string; +}; type OP_ARRAYS_UNIONS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - }[] - }[][] | 'a'[] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - }[] - }[] | 'b'[][] - c?: string -} + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }[]; + }[][] | 'a'[]; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }[]; + b: { + a: 'bba'; + b: 'bbb'; + }[]; + }[] | 'b'[][]; + c?: string; +}; // --------------------------------------------------------------------------------------- // P.MERGE type O_PMERGE = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - x: string - } - } - b?: { - a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - x: string - } - } - c?: string -} + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + x: string; + }; + }; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + x: string; + }; + }; + c?: string; +}; type O_PMERGE_UNIONS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - x: string - } - } | 'a' - b?: { - a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - x: string - } - } | 'b' - c?: string -} - -type O_PMERGE_ARRAYS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - x: string - }[] - }[][] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - x: string - }[] - }[] - c?: string -} - -type O_PMERGE_ARRAYS_UNIONS = { - a: { - a: string - b: { - a: 'aba' - b: 'abb' - x: string - }[] - }[][] | 'a'[] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - x: string - }[] - }[] | 'b'[][] - c?: string -} + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + x: string; + }; + } | 'a'; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + x: string; + }; + } | 'b'; + c?: string; +}; checks([ check, O_PMERGE, Test.Pass>(), check, O_PMERGE_UNIONS, Test.Pass>(), - check, O_PMERGE_ARRAYS, Test.Pass>(), - check, O_PMERGE_ARRAYS_UNIONS, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // P.OMIT type O_POMIT = { - a: { - b: { - a: 'aba' - b: 'abb' - } - } - b?: { - b: { - a: 'bba' - b: 'bbb' - } - } - c?: string -} + a: { + b: { + a: 'aba'; + b: 'abb'; + }; + }; + b?: { + b: { + a: 'bba'; + b: 'bbb'; + }; + }; + c?: string; +}; type O_POMIT_UNIONS = { - a: { - b: { - a: 'aba' - b: 'abb' - } - } | 'a' - b?: { - b: { - a: 'bba' - b: 'bbb' - } - } | 'b' - c?: string -} - -type O_POMIT_ARRAYS = { - a: { - b: { - a: 'aba' - b: 'abb' - }[] - }[][] - b?: { - b: { - a: 'bba' - b: 'bbb' - }[] - }[] - c?: string -} - -type O_POMIT_ARRAYS_UNIONS = { - a: { - b: { - a: 'aba' - b: 'abb' - }[] - }[][] | 'a'[] - b?: { - b: { - a: 'bba' - b: 'bbb' - }[] - }[] | 'b'[][] - c?: string -} + a: { + b: { + a: 'aba'; + b: 'abb'; + }; + } | 'a'; + b?: { + b: { + a: 'bba'; + b: 'bbb'; + }; + } | 'b'; + c?: string; +}; checks([ check, O_POMIT, Test.Pass>(), check, O_POMIT_UNIONS, Test.Pass>(), - check, O_POMIT_ARRAYS, Test.Pass>(), - check, O_POMIT_ARRAYS_UNIONS, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // P.PICK type O_PPICK = { - a: { - a: string - } - b?: { - a: { - a: 'baa' - b: 'bab' - } - } -} + a: { + a: string; + }; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + }; +}; type O_PPICK_UNIONS = { - a: { - a: string - } | 'a' - b?: { - a: { - a: 'baa' - b: 'bab' - } - } | 'b' + a: { + a: string; + } | 'a'; + b?: { + a: { + a: 'baa'; + b: 'bab'; + }; + } | 'b'; +}; + +type O_PPICK_AUTOPATH = { + a: { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + }; +} | { + a: { + a: string; + }; +} | { + b?: { + a: { + b: 'bab'; + }; + } | undefined; } -type O_PPICK_ARRAYS = { - a: { - a: string - }[][] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - }[] -} +const o_ppick_autopath = pick({} as OP, ['a', 'a.a', 'b.a.b']) -type O_PPICK_ARRAYS_UNIONS = { - a: { - a: string - }[][] | 'a'[] - b?: { - a: { - a: 'baa' - b: 'bab' - }[] - }[] | 'b'[][] -} +declare function pick, K extends string>( + obj: T, + keys: F.AutoPath[] +): A.Compute>> checks([ check, O_PPICK, Test.Pass>(), check, O_PPICK_UNIONS, Test.Pass>(), - check, O_PPICK_ARRAYS, Test.Pass>(), - check, O_PPICK_ARRAYS_UNIONS, Test.Pass>(), + check(), ]) // --------------------------------------------------------------------------------------- // P.READONLY type O_PREADONLY = { - a: { - readonly a: string - b: { - a: 'aba' - b: 'abb' - } - } - b?: { - readonly a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - } - } - c?: string -} + a: { + readonly a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + }; + b?: { + readonly a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + }; + }; + c?: string; +}; type O_PREADONLY_UNIONS = { - a: { - readonly a: string - b: { - a: 'aba' - b: 'abb' - } - } | 'a' - b?: { - readonly a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - } - } | 'b' - c?: string -} - -type O_PREADONLY_ARRAYS = { - a: { - readonly a: string - b: { - a: 'aba' - b: 'abb' - }[] - }[][] - b?: { - readonly a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - }[] - }[] - c?: string -} - -type O_PREADONLY_ARRAYS_UNIONS = { - a: { - readonly a: string - b: { - a: 'aba' - b: 'abb' - }[] - }[][] | 'a'[] - b?: { - readonly a: { - a: 'baa' - b: 'bab' - }[] - b: { - a: 'bba' - b: 'bbb' - }[] - }[] | 'b'[][] - c?: string -} + a: { + readonly a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + } | 'a'; + b?: { + readonly a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + }; + } | 'b'; + c?: string; +}; checks([ check, O_PREADONLY, Test.Pass>(), check, O_PREADONLY_UNIONS, Test.Pass>(), - check, O_PREADONLY_ARRAYS, Test.Pass>(), - check, O_PREADONLY_ARRAYS_UNIONS, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- // P.UPDATE type O_PUPDATE = { - a: { - a: 'x' - b: { - a: 'aba' - b: 'abb' - } - } - b?: { - a: 'x' - b: { - a: 'bba' - b: 'bbb' - } - } - c?: string -} + a: { + a: 'x'; + b: { + a: 'aba'; + b: 'abb'; + }; + }; + b?: { + a: 'x'; + b: { + a: 'bba'; + b: 'bbb'; + }; + }; + c?: string; +}; type O_PUPDATE_UNIONS = { - a: 'a' | { - a: 'x' - b: { - a: 'aba' - b: 'abb' - } - }, - b?: 'b' | { - a: 'x' - b: { - a: 'bba' - b: 'bbb' - } - }, - c?: string -} + a: 'a' | { + a: 'x'; + b: { + a: 'aba'; + b: 'abb'; + }; + }, + b?: 'b' | { + a: 'x'; + b: { + a: 'bba'; + b: 'bbb'; + }; + }, + c?: string; +}; type O_PUPDATE_VPATH = { - a: 'a' | { - a: string - b: { - a: 'aba' - b: 'abb' - } - } - b?: 'b' | { - a: { - a: 'baa' - b: 'bab' - } - b: { - a: 'bba' - b: 'bbb' - } - } - c?: string -} + a: 'a' | { + a: string; + b: { + a: 'aba'; + b: 'abb'; + }; + }; + b?: 'b' | { + a: { + a: 'baa'; + b: 'bab'; + }; + b: { + a: 'bba'; + b: 'bbb'; + }; + }; + c?: string; +}; checks([ - check, {a: {b: 'x'}}, Test.Pass>(), check, O_PUPDATE, Test.Pass>(), check, O_PUPDATE_UNIONS, Test.Pass>(), check, O_PUPDATE_VPATH, Test.Pass>(), @@ -1922,48 +1806,48 @@ checks([ // P.RECORD type RECORD_ABCD_STRING_OPTR = { - readonly a?: { - readonly b?: { - readonly d?: string - } - readonly c?: { - readonly d?: string - } - } -} + readonly a?: { + readonly b?: { + readonly d?: string; + }; + readonly c?: { + readonly d?: string; + }; + }; +}; type RECORD_ABCD_STRING_OPTW = { - a?: { - b?: { - d?: string - } - c?: { - d?: string - } - } -} + a?: { + b?: { + d?: string; + }; + c?: { + d?: string; + }; + }; +}; type RECORD_ABCD_STRING_REQR = { - readonly a: { - readonly b: { - readonly d: string - } - readonly c: { - readonly d: string - } - } -} + readonly a: { + readonly b: { + readonly d: string; + }; + readonly c: { + readonly d: string; + }; + }; +}; type RECORD_ABCD_STRING_REQW = { - a: { - b: { - d: string - } - c: { - d: string - } - } -} + a: { + b: { + d: string; + }; + c: { + d: string; + }; + }; +}; checks([ check, RECORD_ABCD_STRING_OPTR, Test.Pass>(), @@ -1971,27 +1855,3 @@ checks([ check, RECORD_ABCD_STRING_REQR, Test.Pass>(), check, RECORD_ABCD_STRING_REQW, Test.Pass>(), ]) - -// type Oo = { -// a: [V] -// b: [[V]] -// c: [[[V]]] -// d: [[[[V]]]] -// e: [[[[[V]]]]] -// f: [[[[[[V]]]]]] -// g: [[[[[[[V]]]]]]] -// h: [[[[[[[[V]]]]]]]] -// } - -// type t1 = O.PatchAll<{}, [ -// Oo<[]>, -// Oo<[1, 2]>, -// Oo<[1, 2, 3]>, -// Oo<[1, 2, 3, 4]>, -// Oo<[1, 2, 3, 4, 5]>, -// Oo<[1, 2, 3, 4, 5, 6]>, -// Oo<[1, 2, 3, 4, 5, 6, 7]>, -// Oo<[1, 2, 3, 4, 5, 6, 7, 8]>, -// Oo<[1, 2, 3, 4, 5, 6, 7, 8, 9]>, -// Oo<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>, -// ], 'deep'> diff --git a/tests/String.ts b/tests/String.ts index 46301f875..1b9b856dc 100644 --- a/tests/String.ts +++ b/tests/String.ts @@ -1,11 +1,48 @@ -import {Test, S} from '../sources/ts-toolbelt' +import {Test, S} from '../sources' const {checks, check} = Test // /////////////////////////////////////////////////////////////////////////////////////// // STRING //////////////////////////////////////////////////////////////////////////////// +type S = + | 'hola' + | 'ciao!' + +// --------------------------------------------------------------------------------------- +// AT + +checks([ + check, 'o' | 'l' | 'i' | 'a', Test.Pass>(), + check, undefined, Test.Pass>(), +]) + +// --------------------------------------------------------------------------------------- +// JOIN + +checks([ + check>, S, Test.Pass>(), +]) + +// --------------------------------------------------------------------------------------- +// LENGTH + +checks([ + check, 4 | 5, Test.Pass>(), +]) + +// --------------------------------------------------------------------------------------- +// REPLACE + +checks([ + check, 'Jane is nice. Jane is good.', Test.Pass>(), +]) + // --------------------------------------------------------------------------------------- -// STRING +// SPLIT -// Cannot be tested +checks([ + check, ['h', 'o', 'l', 'a'] | ['c', 'i', 'a', 'o', '!'], Test.Pass>(), + check, ['a', 'b', 'c'], Test.Pass>(), + check, ['a', 'b', 'c', ''], Test.Pass>(), +]) diff --git a/tests/Union.ts b/tests/Union.ts index 6477b2489..b9683f6df 100644 --- a/tests/Union.ts +++ b/tests/Union.ts @@ -1,4 +1,4 @@ -import {Test, U} from '../sources/ts-toolbelt' +import {Test, U} from '../sources' const {checks, check} = Test @@ -16,7 +16,7 @@ checks([ // EXCLUDE checks([ - check, 1| 2, Test.Pass>(), + check, 1 | 2, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- @@ -36,7 +36,7 @@ checks([ // INTERSECT checks([ - check, 2 | 1, Test.Pass>(), + check, {a: 1} | 1, Test.Pass>(), ]) // --------------------------------------------------------------------------------------- @@ -46,14 +46,6 @@ checks([ check, 1 & 2 & 3, Test.Pass>(), ]) -// --------------------------------------------------------------------------------------- -// KEYS - -checks([ - check, 'a' | 'b', Test.Pass>(), - check<(keyof ({a: 0} | {b: 0})), 'a' & 'b', Test.Pass>(), -]) - // --------------------------------------------------------------------------------------- // LAST @@ -72,14 +64,14 @@ checks([ // --------------------------------------------------------------------------------------- // MERGE -type U_MERGE = {a: string, e: 22} | {b?: number, c: 42} | {b?: string, c?: 48, d: 21, e: 23} +type U_MERGE = {a: string, e: 22} | {b?: number, c: 42} | {b?: string, c?: 48, d: 21, e: 23}; type MERGE_U = { - a: string - b?: string | number - c: 42 | 48 - d: 21 - e: 22 | 23 -} + a: string; + b?: string | number; + c: 42 | 48; + d: 21; + e: 22 | 23; +}; checks([ check, MERGE_U, Test.Pass>(), diff --git a/tsconfig.json b/tsconfig.json index 92be603e7..7489cc069 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -69,7 +69,7 @@ // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ "strict": false, /* Enable all strict type-checking options. */ // "strictBindCallApply": true, /* Enable strict type checks for the bind, call, and apply methods on function objects. */ - // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + "strictFunctionTypes": true, /* Enable strict checking of function types. */ "strictNullChecks": true, /* Enable strict null checks. */ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ "stripInternal": true, /* Do not emit declarations for code that has an '@internal' annotation. */