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

Add support for nested generics #41600

Closed
5 tasks done
janaszer opened this issue Nov 19, 2020 · 2 comments
Closed
5 tasks done

Add support for nested generics #41600

janaszer opened this issue Nov 19, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@janaszer
Copy link

Suggestion

Please add support for deeply nested generics.

Use Cases

This is mostly useful for React development. We can have higer order components that might take generic class and return generic class. However deeply nested generics are inferred as unknown type.

Also connect from react-redux shows the same behaviour.

Examples

// Imagine I have generic component like this:
class Comp<T> extends React.Component<{ data: T, width: number}> {}
// and this is my generic function - it takes component and returns component (with one property less)
function WithWidth<T extends { width: number }>(component: React.ComponentType<T>): React.ComponentType<Omit<T, 'width'>>{
    ...
}

// here I want to create component based on Comp, but with one property bounded
const CompWithWidth = WithWidth(Comp);

In that example CompWithWidth has type:

React.ComponentType<Pick<{
    data: unknown;
    width: number;
}, "data">>

so data is no longer generic parameter. It is unknown from now on.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@andrewbranch
Copy link
Member

Duplicate of #30650

@andrewbranch andrewbranch marked this as a duplicate of #30650 Nov 24, 2020
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 24, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants