Skip to content

Higher order this parameter inference, like #31116 - #33139

Closed
Jack Bates (jablko) wants to merge 2 commits into
microsoft:mainfrom
jablko:patch-8
Closed

Higher order this parameter inference, like #31116#33139
Jack Bates (jablko) wants to merge 2 commits into
microsoft:mainfrom
jablko:patch-8

Conversation

@jablko

Copy link
Copy Markdown
Contributor

The following works since #31116/be88d53ab37453585b449b6ab1918134087b03cf

function identity<T>(value: T) {
    return value;
}

function call<T, R>(fn: (value: T) => R, value: T) {
    return fn(value);
}

const result: number = call(identity, 1);

however the following is still error TS2322: Type 'unknown' is not assignable to type 'number'.

function identity<T>(value: T) {
    return value;
}

identity.call = function<T, R>(this: (value: T) => R, value: T) {
    return this(value);
};

const result: number = identity.call(1);

Consequently while

const result: bigint[] = [1].map(BigInt);

works, the following

const result: bigint[] = Array.prototype.map.call([1], BigInt);

is error TS2322: Type 'unknown[]' is not assignable to type 'bigint[]'. (with --strictBindCallApply).

The following

Array.prototype.map
    .call(buf, BigInt)
    .reduce((result, byte) => (result << 8n) + byte);

is error TS2571: Object is of type 'unknown'.

This PR extends #31116 to the this parameter so the second case function(this, value) behaves like the first function call(fn, value).

This improves type inference for --strictBindCallApply and generic functions and resolves the errors in both of the above Array.prototype.map.call() examples.

@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 3 times, most recently from 88d37ff to f2ee891 Compare October 9, 2019 16:37
@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 2 times, most recently from 42363a4 to 1dcdbda Compare October 19, 2019 18:34
@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 2 times, most recently from aadc185 to 4065598 Compare November 16, 2019 15:24
@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 5 times, most recently from 2154081 to 14d6b45 Compare November 28, 2019 14:51
@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 3 times, most recently from 8918d43 to b135f09 Compare January 25, 2020 20:23
@sandersn Nathan Shively-Sanders (sandersn) added the For Backlog Bug PRs that fix a backlog bug label Feb 1, 2020
@jablko
Jack Bates (jablko) force-pushed the patch-8 branch 5 times, most recently from fd96594 to b2b2bf8 Compare February 9, 2020 17:19
@sandersn

Copy link
Copy Markdown
Member

Unfortunately, we never finished reviewing this PR. It is pretty old now, so I'm going to close it to reduce the number of open PRs.

@typescript-bot

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants