-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow type parameter constraints to reference other type parameters #2304
Comments
If we allowed type parameter constraints to reference other type parameters this signature might get you what you wanted: function a<PropType, T extends { [idx:string]: PropType }>(obj: T, f: (value: PropType) => PropType) { .. } |
If PropType will be a union type, then yes. |
I wanted to give another example for where this would come in useful in the React bindings. React has an API that looks like createElement<P>(type: { new(): Component<P> }, props: P): ReactElement<P> However, in actuality there is an implicit prop called createElement<P, C extends Component<P>>(type: { new(): C }, props: P & { ref: (component: C) => any }): ReactElement<P> However, the above isn't currently allowed due to this issue. Ideally not only would the above be allowed but also have the type arguments inferred from usage. |
@ahejlsberg is going to see what would be involved in removing the restriction. It may be as simple as removing the error, or there may be deeper problems with circularity that motivated this back in the Ye Olde Compiler days. |
I would really like this as well for strongly typing a function that can merge properties, e.g. _.extends. The intersection syntax can correctly tell the compiler the return type of the function, but it can't infer types for the properties of the arguments that also exist on T. Having T extend U would be sufficient I think. React's setState is another example of a function that merges two objects that can't really be properly typed- the only solution we have so far is to make every property optional and then take T. |
I would like to have some feature that infers any possible types from the given object in the generic function.
For instance I have a function that receives Object, and I want to have a more specific callback than 'any'.
Current code:
I wish I had something with more strict typing.
The text was updated successfully, but these errors were encountered: