-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript output? #174
Comments
I'd be interested in seeing what that looks like. |
Initially probably not that different from the common.js one. I'm still exploring Theo though, so depending on how it works it might be interesting to output an |
Yeah, I think Typescript could be interesting if we can output a format that is useful for everyone. |
Our design system uses TypeScript and I wanted to leverage autocomplete in VSCode when referencing Theo-generated styles/themes in React components. The quickest solution was to auto-generate typings for the JS module using It only generates the primitive types, e.g. |
@petekp that link gave me a 404. |
@trazek Oops, fixed :) |
@petekp Could you walk through how you generated the typings for that? |
@mattfelten I'm currently faking it by registering a custom format ( // Convert styles to JS and sort them by category
const categorizeStyles = (styleMap: ImmutableStyleMap) => {
const styleProps: ThemeStyle[] = styleMap.get('props').toJS()
return chain(styleProps)
.sortBy('name')
.groupBy('category')
.value()
}
// Convert styles into an easily consumable JS object ("theme")
export const createThemeObjectFromStyles = (
styleData: ImmutableStyleMap
): Readonly<Theme> => {
return Object.entries(categorizeStyles(styleData)).reduce(
(acc, [category, styles]) => ({
...acc,
...styles.reduce((acc2, style) => {
return {
...acc2,
[style.name]: style.value,
}
}, {}),
}),
{}
)
}
// Create a `.d.ts` file and inject the theme object with typings export declaration
theo.registerFormat(
'd.ts',
styleMap =>
`export declare const _default:
${JSON.stringify(createThemeObjectFromStyles(styleMap))}
`
) |
Ah that's clever. I ended up registering a new
|
@mattfelten Nice! Before my current method I was using my node build script to change the filename from It would be cool to build TS typing support into Theo, perhaps either as an officially supported format on its own ( |
I'll just chime in and say that I don't have any plans to add TypeScript support, but I'd happily accept a PR for it. |
I'm down to help implement this. Still wondering what exactly folks would expect from this format. Just definitions? Auto-generated types/interfaces? Thinking optionally outputting |
That’s what I would expect. That the output can be packaged and “just work” in typescript projects. I don’t think we would need actual |
I used the following which output a TS declaration which matches the const {
pipe,
get,
map,
camelCase,
method,
sortBy,
join,
} = require("lodash/fp");
const toConstDeclaration = ({ name, value }) =>
`export declare const ${camelCase(name)} = ${JSON.stringify(value)}`;
module.exports = pipe(
method("toJS"),
get("props"),
sortBy("name"),
map(toConstDeclaration),
join(";\n")
); You can then register this in your setup file:
|
I see there's no option for outputting typescript, but formats look relatively straightforward to write. Would you be interested in a typescript formatter?
Thanks!
The text was updated successfully, but these errors were encountered: