Skip to content

Instantly share code, notes, and snippets.

@rileybathurst
Last active June 10, 2024 01:14
Show Gist options
  • Save rileybathurst/e46a2a026308eeabd9e7b50e86b827d9 to your computer and use it in GitHub Desktop.
Save rileybathurst/e46a2a026308eeabd9e7b50e86b827d9 to your computer and use it in GitHub Desktop.
Working on a type safe way of showing variable info
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