Last active
June 10, 2024 01:14
-
-
Save rileybathurst/e46a2a026308eeabd9e7b50e86b827d9 to your computer and use it in GitHub Desktop.
Working on a type safe way of showing variable info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react" | |
interface SpecsTypes { | |
[key: string]: string | number | boolean; | |
} | |
function Specs(keyValuePairs: SpecsTypes) { | |
return ( | |
Object.entries(keyValuePairs).map(([key, value]) => { | |
if (key === 'length' && typeof value === 'number') { | |
return ( | |
<div> | |
<h2>{key}</h2> | |
<h3>{value / 2}</h3> | |
</div> | |
); | |
} | |
if (typeof value === 'boolean') { | |
return ( | |
<div key={key}> | |
<h2>{key}</h2> | |
<h3>Yes</h3> | |
</div> | |
); | |
} | |
return ( | |
<div key={key}> | |
<h2>{key}</h2> | |
<h3>{value}</h3> | |
</div> | |
) | |
}) | |
); | |
} | |
const SpecTestPage = () => { | |
return ( | |
<> | |
test | |
<Specs | |
name="hello" | |
length={12} | |
/> | |
</> | |
) | |
} | |
export default SpecTestPage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment