You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
abstractclassBase<TParams>{privateparams: TParams;//cfr https://github.com/Microsoft/TypeScript/issues/1396}interfaceIParams{optional?: number;}interfaceIParams2{required: number;}classComponentextendsBase<IParams>{}classComponent2extendsBase<IParams2>{}functiontest<TParams>(component: Base<TParams>,params: TParams){}varcomponent=newComponent();varcomponent2=newComponent2();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!
The text was updated successfully, but these errors were encountered:
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.
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.:
TypeScript Version: 2.4.0
Code
Expected behavior:
see comments in the code snippet!
The text was updated successfully, but these errors were encountered: