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

Describe use of generic components with JSX #281

Merged
merged 1 commit into from
May 20, 2017
Merged

Describe use of generic components with JSX #281

merged 1 commit into from
May 20, 2017

Conversation

estaub
Copy link
Contributor

@estaub estaub commented May 20, 2017

Look vaguely familiar?
It should: microsoft/TypeScript#3960 (comment)

@basarat basarat merged commit 3fcc47a into basarat:master May 20, 2017
@basarat
Copy link
Owner

basarat commented May 20, 2017

cheers 🌹

@estaub estaub deleted the patch-2 branch May 21, 2017 02:24
@psegalen
Copy link

Hi, thank you for this, it made my day!
However, I was stuck for another 20 minutes with a TSC error about the specialized component not being "cast-able" as the generic one because I declared a constructor in the generic component! Maybe it would be good to mention that generic components should'nt have a constructor?

@basarat
Copy link
Owner

basarat commented May 22, 2017

However, I was stuck for another 20 minutes with a TSC error about the specialized component not being "cast-able" as the generic one because I declared a constructor in the generic component!

Can you paste an example that gives that error please 🌹

@psegalen
Copy link

psegalen commented May 23, 2017

Sure, here it is:

/** Generic component */
interface ListProps<T> { items: T[] }
class GenericList<T> extends Component<ListProps<T>, any> {
    constructor(props: ListProps<T>) { super(props) }
    render() { return (<div />); }
}
/** Specialization */
interface StringList { new (): GenericList<string> } ;
const StringList = GenericList as StringList;

TSC points out a [ts] Type 'typeof GenericList' cannot be converted to type 'StringList'. on the "as" operator in the last line but deleting the contructor makes the error disappear.

@estaub
Copy link
Contributor Author

estaub commented May 23, 2017

@psegalen With Typescript 2.3.2 and @types/react 15.0.23, I can't reproduce the error, and can't think what else might be different.

But I have a theory about what you're seeing. Would you try this replacement line?

interface StringList { new (props: ListProps<string>): GenericList<string> } ;
//                          ^^^^^^^^^^^^^^^^^^^^^^^^

basarat added a commit that referenced this pull request May 23, 2017
@psegalen
Copy link

Oh, I should have seen it, thanks @estaub!

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