Skip to content

Higher order type parameter promotion fixes #30363

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

Merged
merged 9 commits into from
Mar 18, 2019

Conversation

ahejlsberg
Copy link
Member

This PR makes a couple of tweaks to our higher order type parameter promotion algorithm. Specifically, we don't promote type parameters of a function type argument when the contextual signature doesn't reference type parameters in parameter positions, and we now treat inferences made from parameter positions in type parameter promotion as contravariant.

Fixes #30324.

@weswigham
Copy link
Member

@typescript-bot run dt let's see if this fixes ramda completely~

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 13, 2019

Heya @weswigham, I've started to run the Definitely Typed test suite on this PR at e7afa6c. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member

@ahejlsberg this looks new:

Error in css-tree
Error: /home/vsts/work/1/s/dtslint-runner/DefinitelyTyped/types/css-tree/css-tree-tests.ts:211:1
ERROR: 211:1  expect  Expected type to be:
  List<Test>
got:
  List<Test | null>
ERROR: 219:1  expect  Expected type to be:
  List<Test>
got:
  List<Test | null>

might be unrelated, though, don't know. The other failures are caused by #30334 and fixed by #30375.

@weswigham
Copy link
Member

weswigham commented Mar 13, 2019

Here's the testcase from css-tree:

// @strict: true
interface ListItem<TData> {
    prev: ListItem<TData> | null;
    next: ListItem<TData> | null;
    data: TData;
}
type IteratorFn<TData, TResult, TContext = List<TData>> = (this: TContext, item: TData, node: ListItem<TData>, list: List<TData>) => TResult;
type FilterFn<TData, TResult extends TData, TContext = List<TData>> = (this: TContext, item: TData, node: ListItem<TData>, list: List<TData>) => item is TResult;

declare class List<TData> {
    filter<TContext, TResult extends TData>(fn: FilterFn<TData, TResult, TContext>, context: TContext): List<TResult>;
    filter<TResult extends TData>(fn: FilterFn<TData, TResult>): List<TResult>;
    filter<TContext>(fn: IteratorFn<TData, boolean, TContext>, context: TContext): List<TData>;
    filter(fn: IteratorFn<TData, boolean>): List<TData>;
}
interface Test {
    a: string;
}
const list2 = new List<Test | null>();
const filter1 = list2.filter(function(item, node, list): item is Test {
    this.b; // $ExpectType string
    item; // $ExpectType Test | null
    node; // $ExpectType ListItem<Test | null>
    list; // $ExpectType List<Test | null>
    return !!item;
}, {b: 'c'});
filter1; // $ExpectType List<Test>

the change in expected type of filter1 does seem to be isolated to this branch. It seems like we're inferring Test | null for TResult when we should just be inferring Test - this is likely due to the contravariant parameter inference?

@weswigham
Copy link
Member

weswigham commented Mar 13, 2019

Noooooope, I think it's due to the subtle change in inference for return type predicates.

@weswigham
Copy link
Member

Specifically, we check for a matching parameter index.... except we don't account for this parameters not really counting.

@weswigham
Copy link
Member

I opened a PR against this PR - #30386 - with a fix.

@ahejlsberg
Copy link
Member Author

Latest commit changes IdentifierTypePredicate.parameterIndex to be an index into Signature.parameters. It previously was an index into the parameter list of the declaration from which the signature was obtained, which meant we had latent off-by-one errors in signatures with a this parameter (signatures don't include this parameters in the parameters array). Since types always operate on Signature instances and since it is possible for Signature instances to have no declaration associated with them, it really doesn't make sense to store declaration indices.

@weswigham
Copy link
Member

Wanna add the test from #30386 so we have more coverage?

@ahejlsberg
Copy link
Member Author

@weswigham Sure, I can do that.

@ahejlsberg
Copy link
Member Author

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 18, 2019

Heya @ahejlsberg, I've started to run the Definitely Typed test suite on this PR at 3dd0814. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member

The DT run looks great - just has the instantiation error that was in master back when this branch was first created, so this branch fixes ramda, as intended, and breaks nothing else now. 💓

@ahejlsberg ahejlsberg merged commit 36cf12f into master Mar 18, 2019
@ahejlsberg ahejlsberg deleted the fixTypeParameterPromotion branch March 18, 2019 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants