Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Jul 6, 2023
1 parent 4fdc61a commit de190f7
Show file tree
Hide file tree
Showing 13 changed files with 9,295 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.*
!/typedoc.json
!/src
!/dist
!/docs
!/package.json
!/package-lock.json
Expand Down
2,356 changes: 2,356 additions & 0 deletions dist/cjs/index.d.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/cjs/index.d.ts.map

Large diffs are not rendered by default.

2,601 changes: 2,601 additions & 0 deletions dist/cjs/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/cjs/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
2,356 changes: 2,356 additions & 0 deletions dist/mjs/index.d.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/mjs/index.d.ts.map

Large diffs are not rendered by default.

1,934 changes: 1,934 additions & 0 deletions dist/mjs/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/mjs/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
35 changes: 30 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,37 @@ export {
processDebugPort,
}

/**
* Parameters<T> and ReturnType<T> do not work with overloaded functions.
* see https://github.com/microsoft/TypeScript/issues/32164
* this is one of the suggested workarounds.
*/
type Overloads<T extends (...args: any[]) => any> =
T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3; (...args: infer A4): infer R4; (...args: infer A5): infer R5; (...args: infer A6): infer R6 } ?
((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5) | ((...args: A6) => R6)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3; (...args: infer A4): infer R4; (...args: infer A5): infer R5 } ?
((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3; (...args: infer A4): infer R4 } ?
((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3 } ?
((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2 } ?
((...args: A1) => R1) | ((...args: A2) => R2)
: T extends { (...args: infer A1): infer R1 } ?
(...args: A1) => R1
: never

type OverloadedParameters<T extends (...args: any[]) => any> = Parameters<Overloads<T>>;
type OverloadedReturnType<T extends (...args: any[]) => any> = ReturnType<Overloads<T>>;



export type UncurryThis<
T extends (this: unknown, ...args: unknown[]) => unknown
> = (self: ThisParameterType<T>, ...args: Parameters<T>) => ReturnType<T>
> = (self: ThisParameterType<T>, ...args: OverloadedParameters<T>) => OverloadedReturnType<T>
export type UncurryThisStaticApply<
T extends (this: unknown, ...args: unknown[]) => unknown
> = (self: ThisParameterType<T>, args: Parameters<T>) => ReturnType<T>
> = (self: ThisParameterType<T>, args: OverloadedParameters<T>) => OverloadedReturnType<T>

export type StaticCall<
T extends (this: unknown, ...args: unknown[]) => unknown
Expand Down Expand Up @@ -1132,7 +1157,7 @@ const TypedArrayFrom: TypedArrayFrom = <T extends TypedArray>(
if (!fn) {
throw new TypeError('invalid TypedArray constructor: ' + ctor)
}
return uncurryThis(fn)(ctor, ...args) as T
return (uncurryThis(fn) as any)(ctor, ...args) as T
}

const SafeObject = OBJECT.defineProperties(
Expand Down Expand Up @@ -1216,7 +1241,7 @@ const uncurryGetter = <O extends object, K extends keyof O, T = O>(
): UncurryGetter<O, K, T> => {
const desc = SafeReflect.getOwnPropertyDescriptor(obj, k)
if (desc?.get) {
return uncurryThis(desc.get)
return uncurryThis(desc.get) as any;
}
throw new Error('invalid uncurryGetter call: ' + String(k))
}
Expand All @@ -1227,7 +1252,7 @@ const uncurrySetter = <O extends object, K extends keyof O, T = O>(
): UncurrySetter<O, K, T> => {
const desc = SafeReflect.getOwnPropertyDescriptor(obj, k)
if (desc?.set) {
return uncurryThis(desc.set) as UncurrySetter<O, K, T>
return uncurryThis(desc.set) as unknown as UncurrySetter<O, K, T>
}
throw new Error('invalid uncurrySetter call: ' + String(k))
}
Expand Down
8 changes: 7 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import t from 'tap'
import * as primordialsNamed from '../'
import { primordials } from '../'
import { primordials, StringPrototypeReplace } from '../'

import { promisify } from 'util'
const utilPromisifyCustom = promisify.custom
Expand Down Expand Up @@ -426,3 +426,9 @@ t.test('makeSafe', t => {

t.end();
});

t.test('StringPrototypeReplace sanity', t => {
t.equal(primordials.StringPrototypeReplace('foo', 'o', 'a'), 'fao');
t.equal(StringPrototypeReplace('foo', 'o', 'a'), 'fao');
t.end()
});

0 comments on commit de190f7

Please sign in to comment.