GitHub - LeDDGroup/typescript-transform-jsx: Typescript transform jsx to string (original) (raw)

typescript-transform-jsx

npm version Greenkeeper badge Maintainability

Conventional Commits code style: prettier Built with Spacemacs

Typescript transform jsx to string

Table of Contents

Motivation

See examples

Install

$ npm i -D typescript-transform-jsx

Usage with ttypescript

Add it to plugins in your tsconfig.json

{ "compilerOptions": { "jsx": "react-native", "plugins": [{ "transform": "typescript-transform-jsx" }] } }

See https://github.com/LeDDGroup/typescript-transform-jsx/tree/master/examples/example-ttypescript

Setup

Set the jsx flag to react-native or preserve in your tsconfig file. Then create a types.ts with the following content:

declare namespace JSX { type Element = string; interface ElementChildrenAttribute { children: any; } interface IntrinsicElements { [element: string]: { [property: string]: any; }; } }

This will declare custom JSX so you don't need react typings.

Example

interface Person { name: string; age: number; }

const App = (props: { persons: Person[] }) => (

);

Gets compiled to:

const App = props => <ul>${props.persons .map(person =>

  • ${person.name} is ${person.age} years old
  • ) .join("")}</ul>;

    Roadmap/Caveats

    // input const props = { class: "container" };

    ; // output const props = { class: "container" }; `
    `${key}="${value}"` )}>
    `;

    Contributing

    If you have any question or idea of a feature create an issue in github or make an PR.