Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher order this parameter inference, like #31116 #33139

Closed
wants to merge 2 commits into from

Conversation

jablko
Copy link
Contributor

@jablko jablko commented Aug 29, 2019

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 jablko force-pushed the patch-8 branch 3 times, most recently from 88d37ff to f2ee891 Compare October 9, 2019 16:37
@jablko jablko force-pushed the patch-8 branch 2 times, most recently from 42363a4 to 1dcdbda Compare October 19, 2019 18:34
@jablko jablko force-pushed the patch-8 branch 2 times, most recently from aadc185 to 4065598 Compare November 16, 2019 15:24
@jablko jablko force-pushed the patch-8 branch 5 times, most recently from 2154081 to 14d6b45 Compare November 28, 2019 14:51
@jablko jablko force-pushed the patch-8 branch 3 times, most recently from 8918d43 to b135f09 Compare January 25, 2020 20:23
@jablko jablko requested a review from ahejlsberg January 25, 2020 20:58
@sandersn sandersn added the For Backlog Bug PRs that fix a backlog bug label Feb 1, 2020
@jablko jablko force-pushed the patch-8 branch 4 times, most recently from c96c0d8 to fd96594 Compare February 6, 2020 14:56
@jablko jablko force-pushed the patch-8 branch 6 times, most recently from 02c53f0 to 84cb8d9 Compare February 13, 2020 15:42
@sandersn
Copy link
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.

@sandersn sandersn closed this May 24, 2022
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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