yum!
{/* Otherwise tag is optional,
if not provided, null will be rendered /}
[](https://github.com/KonstantinSimeonov/tsx-control-statements/actions/workflows/ci.yml) [ 

Basically jsx-control-statements, but for the typescript compiler toolchain. Works for both javascript and typescript.
| Typescript version range | tsx-control-statements version |
|:------------------------:|:-------------------------------------------|
| 2.4.x - 3.3.x | v3.3.x |
| 3.4.x - 4.6.x | v4.x |
| 4.9 | v5.0 |
| 5.x | >= v5.1 |
.tsx files.js and .jsx files"allowJs" should be set to true in your typescript configurationnpm i && npm run build && npm run test. It includes:jsx-control-statements (i.e. both produce the same output html)2.4.xcreate-react-app scaffold project without ejecting the scripts and modifying themtsc, ts-register, ts-node) feature no flag (that I know of) that allows for addition of custom transformersisolatedModules flag currently causes build errors for typescript files, since the typings currently live in a namespace~~isolatedModules is supported since the module tsx-control-statements/components contains stub definitions which can be imported import { For, If } from 'tsx-control-statements/components'@babel/preset-typescript@babel/plugin-transform-typescript``tsx
import { If } from 'tsx-control-statements/components';
const SongRelatedThingy = ({ songList }: { songList: string[] }) => (
good taste in music
// will transpile to
const SongRelatedThingy = ({ songList }) => (
{songList.includes('Gery-Nikol - Im the Queen')
? 'good taste in music'
: null}
$3
`tsx
import { With } from 'tsx-control-statements/components';const Sum = () => (
{a + b + c}
);// becomes
const Sum = () =>
{((a, b, c) => a + b + c)(3, 5, 6)}
;
`$3
More flexible than [].map, since it can be provided with an iterator or an array-like as it's first parameter. For non-legacy code, prefer the more type-safe alternative.
`tsx
import { For } from 'tsx-control-statements/components';// more type-safe for, the typechecker knows
// the types of the "name" and "i" bindings
const Names = ({ names }: { names: string[] }) => (
of={names}
body={(name, i) => (
{i}
{name}
)}
/>
);// jsx-control-statements compatible
const Names = ({ names }: { names: string[] }) => (
{i}
{name}
);// both of the above will transpile to:
const Names = ({ names }) => (
{Array.from(names, (name, i) => (
{i}
{name}
))}
);
`$3
`tsx
import {
Choose,
When,
Otherwise
} from 'tsx-control-statements/components';const RandomStuff = ({ str }: { str: string }) => (
ivancho
yum!
{/* Otherwise tag is optional,
if not provided, null will be rendered /}
im the queen da da da da
);
// transpiles to
const RandomStuff = ({ str }) => (
{str === 'ivan'
? 'ivancho'
: str === 'sarmi'
? React.createElement('h1', null, 'yum!')
: 'im the queen da da da da'}
);
`Cookbook
#### Bundlers and scaffolding tools
-
webpack with ts-loader
- rollup with typescript plugin
- parcel - this might work but don't count on it#### Testing
-
ava, mocha or anything other that can use ts-node - ts-node supports programatically adding custom transformers so it can be used to run test suites.
- jest using ts-jest like that#### Importing the transformer in your build configs:
`ts
// commonjs
const transformer = require('tsx-control-statements').default;// ts
import transformer from 'tsx-control-statements';
`#### Importing type definitions:
`ts
import {
For,
If,
With,
Choose,
When,
Otherwise
} from 'tsx-control-statements/components';
`Reasons to not use any control statements for jsx:
- ~~Hard to statically type~~
- Has been somewhat adressed, with the exception of With`