Repository navigation
Replies: 1 comment
|
A single import { match } from 'ts-pattern';
function Hangman({ limbsCount }: { limbsCount: number }) {
const parts = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg'];
return <>{parts.map((part, index) => match(limbsCount)
.when(n => n >= index + 1, () => <span key={part}>{part}</span>)
.otherwise(() => null))}</>;
}Replace the sample spans with your Tested the exact component with ts-pattern 5.9.0, TypeScript 5.9.3 and React 18.2.0: counts 0 through 6 rendered the expected accumulated parts. First matching branch. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hey team, this is a great lib I use all the time.
I wonder how to match multiple results, e.g. in a hangman game:
I'd like to display the full hangman when it's
gte(6), but since the match is exclusive, it is only displaying the head.Is this possible to achieve with
ts-pattern?All reactions