Skip to content

Commit

Permalink
🐞 fix: Fix type error in TypeScript 5.5
Browse files Browse the repository at this point in the history
Fixed "Type instantiation is excessively deep and possibly infinite" error caused by `NameParams` in TypeScript 5.5.
  • Loading branch information
Snowflyt committed Apr 27, 2024
1 parent 6b87ab0 commit b4361f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "safunc",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"description": "Create runtime-validated functions with ease, supporting optional parameters and overloaded signatures with smart type inference in TypeScript",
"keywords": [
Expand Down
17 changes: 11 additions & 6 deletions src/tools/name-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ interface Otherwise<Names extends _[]> {

type Conditions = [...[Condition<any, any>, _[], _[]][], Otherwise<any>];

type CountEq<X, TS extends unknown[], Acc extends number = 0> =
type CountEq<X, TS extends unknown[]> = _CountEq<X, TS> extends infer R extends number ? R : never;
type _CountEq<X, TS extends unknown[], Acc extends number = 0> =
TS extends [] ? Acc
: CountEq<X, TailPart<TS>, Eq<PartElem<HeadPart<TS>>, X> extends true ? Inc<Acc> : Acc>;
type CountEqOneOf<XS extends unknown[], TS extends unknown[], Acc extends number = 0> =
: _CountEq<X, TailPart<TS>, Eq<PartElem<HeadPart<TS>>, X> extends true ? Inc<Acc> : Acc>;
type CountEqOneOf<XS extends unknown[], TS extends unknown[]> =
_CountEqOneOf<XS, TS> extends infer R extends number ? R : never;
type _CountEqOneOf<XS extends unknown[], TS extends unknown[], Acc extends number = 0> =
TS extends [] ? Acc
: CountEqOneOf<
: _CountEqOneOf<
XS,
TailPart<TS>,
_EqOneOf<PartElem<HeadPart<TS>>, XS> extends true ? Inc<Acc> : Acc
Expand All @@ -89,9 +92,11 @@ type _EqOneOf<T, XS extends unknown[]> =
true
: _EqOneOf<T, Rest>
: false;
type CountExtends<X, TS extends unknown[], Acc extends number = 0> =
type CountExtends<X, TS extends unknown[]> =
_CountExtends<X, TS> extends infer R extends number ? R : never;
type _CountExtends<X, TS extends unknown[], Acc extends number = 0> =
TS extends [] ? Acc
: CountExtends<X, TailPart<TS>, PartElem<HeadPart<TS>> extends X ? Inc<Acc> : Acc>;
: _CountExtends<X, TailPart<TS>, PartElem<HeadPart<TS>> extends X ? Inc<Acc> : Acc>;

type CountOtherwise<TS extends unknown[], CS extends unknown[], Acc extends number = 0> =
TS extends [] ? Acc
Expand Down
10 changes: 8 additions & 2 deletions src/tools/number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CompareStrLength, Reverse, StrToNum } from "./string";

export type Ordering = LT | EQ | GT;
export type EQ = 0;
export type GT = 1;
export type LT = -1;
Expand Down Expand Up @@ -74,8 +75,13 @@ type _AddDigit<N extends Digit, M extends Digit> =
* Compare two natural numbers.
*/
export type Compare<N extends number, M extends number> =
_CompareListLength<_ToChars<N>, _ToChars<M>> extends EQ ? _CompareDigits<_ToChars<N>, _ToChars<M>>
: _CompareListLength<_ToChars<N>, _ToChars<M>>;
(
_CompareListLength<_ToChars<N>, _ToChars<M>> extends EQ ?
_CompareDigits<_ToChars<N>, _ToChars<M>>
: _CompareListLength<_ToChars<N>, _ToChars<M>>
) extends infer R extends Ordering ?
R
: never;
type _CompareListLength<NS extends unknown[], MS extends unknown[]> =
NS extends [unknown, ...infer ATail extends unknown[]] ?
MS extends [unknown, ...infer BTail extends unknown[]] ?
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
# yarn lockfile v1


"@aashutoshrathi/word-wrap@^1.2.3":
version "1.2.6"
resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf"
integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==

"@ampproject/remapping@^2.2.1":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.3.0.tgz#ed441b6fa600072520ce18b43d2c8cc8caecc7f4"
Expand Down Expand Up @@ -2847,16 +2842,16 @@ onetime@^6.0.0:
mimic-fn "^4.0.0"

optionator@^0.9.3:
version "0.9.3"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.3.tgz#007397d44ed1872fdc6ed31360190f81814e2c64"
integrity sha512-JjCoypp+jKn1ttEFExxhetCKeJt9zhAgAve5FXHixTvFDW/5aEktX9bufBKLRRMdU7bNtpLfcGu94B3cdEJgjg==
version "0.9.4"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.4.tgz#7ea1c1a5d91d764fb282139c88fe11e182a3a734"
integrity sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==
dependencies:
"@aashutoshrathi/word-wrap" "^1.2.3"
deep-is "^0.1.3"
fast-levenshtein "^2.0.6"
levn "^0.4.1"
prelude-ls "^1.2.1"
type-check "^0.4.0"
word-wrap "^1.2.5"

p-event@^5.0.1:
version "5.0.1"
Expand Down Expand Up @@ -3094,9 +3089,9 @@ queue-microtask@^1.2.2:
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==

react-is@^18.0.0:
version "18.3.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.0.tgz#6c2d9b6cdd4c2cffb7c89b1bcb57bc44d12f1993"
integrity sha512-wRiUsea88TjKDc4FBEn+sLvIDesp6brMbGWnJGjew2waAc9evdhja/2LvePc898HJbHw0L+MTWy7NhpnELAvLQ==
version "18.3.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz#e83557dc12eae63a99e003a46388b1dcbb44db7e"
integrity sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==

readdirp@~3.6.0:
version "3.6.0"
Expand Down Expand Up @@ -3812,6 +3807,11 @@ why-is-node-running@^2.2.2:
siginfo "^2.0.0"
stackback "0.0.2"

word-wrap@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
Expand Down

0 comments on commit b4361f1

Please sign in to comment.