Skip to content

Describe use of generic components with JSX #281

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

Merged
merged 1 commit into from
May 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/jsx/tsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ const bar: React.ReactElement<MyAwesomeComponent> = <NotMyAwesomeComponent/>; //

> Of course you can use this as a function argument annotation and even React component prop member.

### React JSX Tip: Generic components
There's no syntax in JSX to apply generic parameters to a generic component; a type and constructor must first be created in Typescript proper. Example:

```ts
/** A generic component */
type SelectProps<T> = { items: T[] }
class Select<T> extends React.Component<SelectProps<T>, any> { }

/** Specialize Select to use with strings */
interface IStringSelect { new (): Select<string> } ;
/** Constructor function - must be capitalized for JSX
const StringSelect = Select as IStringSelect;

/** Usage */
const Form = ()=> <StringSelect items={['a','b']} />;
```
## Non React JSX
TypeScript provides you with the ability to use something other than React with JSX in a type safe manner. The following lists the customizability points, but note that this is for advanced UI framework authors:

Expand Down