Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions types/call-bind/call-bind-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './test/callBind.test';
import './test/callBound.test';
21 changes: 21 additions & 0 deletions types/call-bind/callBound.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This is necessary to disallow import of `call-bind/callBound.js`:
// tslint:disable-next-line: no-declare-current-package no-single-declare-module
declare module 'call-bind/callBound' {
import type { Intrinsics } from 'get-intrinsic';

type PrependThisParameter<T> = T extends (...args: infer A) => infer R
? (thisArg: ThisParameterType<T>, ...args: A) => R
: T;

export = callBoundIntrinsic;

function callBoundIntrinsic<K extends keyof Intrinsics>(
name: K,
allowMissing?: false,
): PrependThisParameter<Intrinsics[K]>;
function callBoundIntrinsic<K extends keyof Intrinsics>(
name: K,
allowMissing?: boolean,
): PrependThisParameter<Intrinsics[K]> | undefined;
function callBoundIntrinsic(name: string, allowMissing?: boolean): any;
}
64 changes: 64 additions & 0 deletions types/call-bind/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Type definitions for call-bind 1.0
// Project: https://github.com/ljharb/call-bind#readme
// Definitions by: Jordan Harband <https://github.com/ljharb>
// ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.9

// This is necessary to disallow import of `call-bind/index` or `call-bind/index.js`:
// tslint:disable-next-line: no-declare-current-package no-single-declare-module
declare module 'call-bind' {
export = callBind;

/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
*
* Equivalent to:
* ```js
* Function.prototype.call.bind(target, ...)
* ```
*
* @param target The function to be used as the this object for `Function.prototype.call`.
* @param args Arguments to bind to the parameters of the function.
*/
function callBind<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
): (thisArg: T, ...args: A) => R;
function callBind<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
thisArg: T,
): (...args: A) => R;
function callBind<T, AX extends readonly unknown[], A extends readonly unknown[], R>(
originalFunction: (this: T, ...args: readonly [...bound: AX, ...args: A]) => R,
thisArg: T,
...bound: AX
): (...args: A) => R;

namespace callBind {
/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
*
* Equivalent to:
* ```js
* Function.prototype.apply.bind(target, ...)
* ```
*
* @param target The function to be used as the this object for `Function.prototype.apply`.
* @param args Arguments to bind to the parameters of the function.
*/
function apply<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
): (thisArg: T, args: Readonly<A>) => R;
function apply<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
thisArg: T,
): (args: Readonly<A>) => R;
function apply<T, A1 extends readonly unknown[], A2 extends readonly unknown[], R>(
originalFunction: (this: T, ...args: readonly [...A1, ...A2]) => R,
thisArg: T,
...args: A1
): (args: Readonly<A2>) => R;
}
}
7 changes: 7 additions & 0 deletions types/call-bind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"types": "index",
"typesVersions": {
"<=3.9": { "*": ["ts3.9/*"] }
}
}
24 changes: 24 additions & 0 deletions types/call-bind/test/callBind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import callBind = require('call-bind');
import { apply as applyBind } from 'call-bind';

declare const any: unknown;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this instead of just using unknown directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because unknown only refers to a type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, so it could be named anything then. i was just confused by the name any

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, any is a super confusing name for a value of type unknown. Maybe try unknown ?


callBind(() => {}); // $ExpectType (thisArg: unknown) => void
callBind((a: string, b: number) => {}, null, 'foo'); // $ExpectType (b: number) => void

// $ExpectType (thisArg: unknown, v: string | number | symbol) => boolean || (thisArg: unknown, v: PropertyKey) => boolean
callBind(Object.prototype.hasOwnProperty);
// $ExpectType (v: string | number | symbol) => boolean || (v: PropertyKey) => boolean
callBind(Object.prototype.hasOwnProperty, any);

applyBind(() => {}); // $ExpectType (thisArg: unknown, args: readonly []) => void
applyBind((a: string, b: number) => {}, null, 'foo'); // $ExpectType (args: readonly [b: number]) => void

// $ExpectType (thisArg: unknown, args: readonly [v: string | number | symbol]) => boolean || (thisArg: unknown, args: readonly [v: PropertyKey]) => boolean
applyBind(Object.prototype.hasOwnProperty);
// $ExpectType (args: readonly [v: string | number | symbol]) => boolean || (args: readonly [v: PropertyKey]) => boolean
applyBind(Object.prototype.hasOwnProperty, any);

// These are invalid because of `package.json#exports`:
import callBindIllegal1 = require('call-bind/index'); // $ExpectError
import callBindIllegal2 = require('call-bind/index.js'); // $ExpectError
19 changes: 19 additions & 0 deletions types/call-bind/test/callBound.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import callBound = require('call-bind/callBound');

callBound('%ArrayProto_keys%'); // $ExpectType (thisArg: unknown) => IterableIterator<number>
callBound('%ArrayProto_values%'); // $ExpectType (thisArg: unknown) => IterableIterator<any>
callBound('%ArrayProto_entries%'); // $ExpectType (thisArg: unknown) => IterableIterator<[number, any]>
callBound('%ArrayProto_forEach%'); // $ExpectType (thisArg: unknown, callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any) => void

callBound('%ObjProto_toString%'); // $ExpectType (thisArg: unknown) => string
callBound('%ObjProto_valueOf%'); // $ExpectType (thisArg: unknown) => Object

// $ExpectType (thisArg: unknown, onfulfilled?: ((value: any) => unknown) | null | undefined, onrejected?: ((reason: any) => unknown) | null | undefined) => Promise<unknown>
callBound('%PromiseProto_then%');

// Dotted intrinsic:
// $ExpectType (thisArg: unknown, v: string | number | symbol) => boolean || (thisArg: unknown, v: PropertyKey) => boolean
callBound('%Object.prototype.hasOwnProperty%');

// These are invalid because of `package.json#exports`:
import callBoundIllegal1 = require('call-bind/callBound.js'); // $ExpectError
2 changes: 2 additions & 0 deletions types/call-bind/ts3.9/call-bind-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './test/callBind.test';
import './test/callBound.test';
155 changes: 155 additions & 0 deletions types/call-bind/ts3.9/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// This is necessary to disallow import of `call-bind/index` or `call-bind/index.js`:
// tslint:disable-next-line: no-declare-current-package no-single-declare-module
declare module 'call-bind' {
export = callBind;

/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
*
* Equivalent to:
* ```js
* Function.prototype.call.bind(target, ...)
* ```
*
* @param target The function to be used as the this object for `Function.prototype.call`.
* @param args Arguments to bind to the parameters of the function.
*/
function callBind<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
): (thisArg: T, ...args: A) => R;
function callBind<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
thisArg: T,
): (...args: A) => R;
function callBind<T, A0, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, ...args: A) => R,
thisArg: T,
arg0: A0,
): (...args: A) => R;
function callBind<T, A0, A1, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
): (...args: A) => R;
function callBind<T, A0, A1, A2, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
arg2: A2,
): (...args: A) => R;
function callBind<T, A0, A1, A2, A3, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
arg2: A2,
arg3: A3,
): (...args: A) => R;
function callBind<T, AX, R>(
target: (this: T, ...args: readonly AX[]) => R,
thisArg: T,
...args: readonly AX[]
): (...args: readonly AX[]) => R;

// tslint:disable-next-line: ban-types
function callBind<F extends Function>(
target: F,
): (
thisArg: ThisParameterType<F>,
...args: F extends (...args: infer A) => any ? A : readonly unknown[]
) => F extends (...args: any) => infer R ? R : any;

// tslint:disable-next-line: ban-types
function callBind<F extends Function>(
target: F,
thisArg: ThisParameterType<F>,
): (
...args: F extends (...args: infer A) => any ? A : readonly unknown[]
) => F extends (...args: any) => infer R ? R : any;

// tslint:disable-next-line: ban-types
function callBind<F extends Function>(
target: F,
thisArg: ThisParameterType<F>,
...args: readonly unknown[]
): (...args: readonly unknown[]) => F extends (...args: any) => infer R ? R : any;

namespace callBind {
/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
*
* Equivalent to:
* ```js
* Function.prototype.apply.bind(target, ...)
* ```
*
* @param target The function to be used as the this object for `Function.prototype.apply`.
* @param args Arguments to bind to the parameters of the function.
*/
function apply<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
): (thisArg: T, args: Readonly<A>) => R;
function apply<T, A extends readonly unknown[], R>(
target: (this: T, ...args: A) => R,
thisArg: T,
): (args: Readonly<A>) => R;
function apply<T, A0, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, ...args: A) => R,
thisArg: T,
arg0: A0,
): (args: Readonly<A>) => R;
function apply<T, A0, A1, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
): (args: Readonly<A>) => R;
function apply<T, A0, A1, A2, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
arg2: A2,
): (args: Readonly<A>) => R;
function apply<T, A0, A1, A2, A3, A extends readonly unknown[], R>(
target: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
thisArg: T,
arg0: A0,
arg1: A1,
arg2: A2,
arg3: A3,
): (args: Readonly<A>) => R;
function apply<T, AX, R>(
target: (this: T, ...args: readonly AX[]) => R,
thisArg: T,
...args: readonly AX[]
): (args: readonly AX[]) => R;

// tslint:disable-next-line: ban-types
function apply<F extends Function>(
target: F,
): (
thisArg: ThisParameterType<F>,
args: F extends (...args: infer A) => any ? Readonly<A> : readonly unknown[],
) => F extends (...args: any) => infer R ? R : any;

// tslint:disable-next-line: ban-types
function apply<F extends Function>(
target: F,
thisArg: ThisParameterType<F>,
): (
args: F extends (...args: infer A) => any ? Readonly<A> : readonly unknown[],
) => F extends (...args: any) => infer R ? R : any;

// tslint:disable-next-line: ban-types
function apply<F extends Function>(
target: F,
thisArg: ThisParameterType<F>,
...args: readonly unknown[]
): (args: ArrayLike<unknown>) => F extends (...args: any) => infer R ? R : any;
}
}
14 changes: 14 additions & 0 deletions types/call-bind/ts3.9/test/callBind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import callBind = require('call-bind');
import { apply as applyBind } from 'call-bind';

declare const any: unknown;

callBind(() => {}); // $ExpectType (thisArg: unknown) => void
callBind((a: string, b: number) => {}, null, 'foo'); // $ExpectType (b: number) => void
callBind(Object.prototype.hasOwnProperty); // $ExpectType (thisArg: unknown, v: string | number | symbol) => boolean
callBind(Object.prototype.hasOwnProperty, any); // $ExpectType (v: string | number | symbol) => boolean

applyBind(() => {}); // $ExpectType (thisArg: unknown, args: readonly []) => void
applyBind((a: string, b: number) => {}, null, 'foo'); // $ExpectType (args: readonly [number]) => void
applyBind(Object.prototype.hasOwnProperty); // $ExpectType (thisArg: unknown, args: readonly [string | number | symbol]) => boolean
applyBind(Object.prototype.hasOwnProperty, any); // $ExpectType (args: readonly [string | number | symbol]) => boolean
19 changes: 19 additions & 0 deletions types/call-bind/ts3.9/test/callBound.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import callBound = require('call-bind/callBound');

callBound('%ArrayProto_keys%'); // $ExpectType (thisArg: unknown) => IterableIterator<number>
callBound('%ArrayProto_values%'); // $ExpectType (thisArg: unknown) => IterableIterator<any>
callBound('%ArrayProto_entries%'); // $ExpectType (thisArg: unknown) => IterableIterator<[number, any]>
callBound('%ArrayProto_forEach%'); // $ExpectType (thisArg: unknown, callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any) => void

callBound('%ObjProto_toString%'); // $ExpectType (thisArg: unknown) => string
callBound('%ObjProto_valueOf%'); // $ExpectType (thisArg: unknown) => Object

// $ExpectType (thisArg: unknown, onfulfilled?: ((value: any) => unknown) | null | undefined, onrejected?: ((reason: any) => unknown) | null | undefined) => Promise<unknown>
callBound('%PromiseProto_then%');

// Dotted intrinsic:
// $ExpectType (thisArg: unknown, v: string | number | symbol) => boolean
callBound('%Object.prototype.hasOwnProperty%');

// These are invalid because of `package.json#exports`:
import callBoundIllegal1 = require('call-bind/callBound.js'); // $ExpectError
19 changes: 19 additions & 0 deletions types/call-bind/ts3.9/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["ESNext"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": ["../../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"call-bind-tests.ts",
"index.d.ts"
]
}
1 change: 1 addition & 0 deletions types/call-bind/ts3.9/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
19 changes: 19 additions & 0 deletions types/call-bind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["ESNext"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"call-bind-tests.ts",
"index.d.ts"
]
}
1 change: 1 addition & 0 deletions types/call-bind/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }