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

Only optional properties of generic parameter are inferred #17654

Closed
ownerer opened this issue Aug 7, 2017 · 2 comments
Closed

Only optional properties of generic parameter are inferred #17654

ownerer opened this issue Aug 7, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ownerer
Copy link

ownerer commented Aug 7, 2017

TypeScript Version: 2.4.0

Code

abstract class Base<TParams> {
    private params: TParams; //cfr https://github.com/Microsoft/TypeScript/issues/1396
}
interface IParams {
    optional?: number;
}
interface IParams2 {
    required: number;
}
class Component extends Base<IParams> {

}
class Component2 extends Base<IParams2> {

}

function test<TParams>(component: Base<TParams>, params: TParams) {
}

var component = new Component();
var component2 = new Component2();

test(component, {  }); //<-- property "optional" of interface "IParams" is inferred correctly (run this in Playground and press ctrl+space with the cursor in the brackets)
test(component2, {  }); //<-- property "required" of interface "IParams2" is NOT inferred at all. It IS inferred when you make optional as well!

Expected behavior:
see comments in the code snippet!

@ownerer
Copy link
Author

ownerer commented Aug 8, 2017

As mentioned in #17655: changing

function test<TParams>(component: Base<TParams>, params: TParams) {
}

To

function test<TParams>(component: Base<TParams>, params: TParams & {}) {
}

Makes the compiler throw an error on the missing "required" property of interface "IParams2".
So as far as enforcing types is concerned, this works.
However, intellisense still does not suggest the property "required" when pressing ctrl+space in the object literal!
It DOES when that property is made optional.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 22, 2017

This is the same underlying issue that #14829 is addressing. All references to a generic type parameters are used to infer a type from the invocation; in some cases, you want to infer from one, and just check in another. the & {} trick makes it so that you have a lower priority inference and thus use the inference from the other reference only.
another way of doing this is using two type parameters, e.g.:

function test<T, U extends T>(component: Base<T>, params: U) {
}

closing in favor of #14829

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 22, 2017
@mhegazy mhegazy closed this as completed Aug 22, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants