Improve TypeScript support for media templates
See original GitHub issueTyping media templates can be cumbersome. I’ve spent a fair amount of time to figure out the following type declarations and casts compliant with TypeScript’s strict mode:
import { ThemedCssFunction } from 'styled-components';
import { css } from './styled-components';
import ThemeInterface from './theme';
const sizes = {
small: 600,
medium: 1024,
large: 1440,
xlarge: 1920,
};
// Iterate through the sizes and create a media template
export const media = (Object.keys(sizes) as (keyof typeof sizes)[]).reduce(
(acc, label) => {
acc[label] = (first: any, ...interpolations: any[]) => css`
@media (min-width: ${sizes[label]}px) {
${css(first, ...interpolations)}
}
`;
return acc;
},
{} as { [key in keyof typeof sizes]: ThemedCssFunction<ThemeInterface> },
);
One of the possible solutions is writing docs including the aforementioned code snippet. An other way to get around the issue and lower the amount of boilerplate code required even for vanilla JS projects is to create a method which encapsulates the functionality above:
import { mediaTemplates } from 'styled-components';
const media = mediaTemplates({
small: 600,
medium: 1024,
large: 1440,
xlarge: 1920,
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:36
- Comments:13 (3 by maintainers)
Top Results From Across the Web
Modernizing Our Image Template Service with React ...
Modernizing Our Image Template Service with React, TypeScript, Chrome, and Kubernetes - How and Why We Re-wrote Our Busiest Service.
Read more >Improving the TypeScript support in Azure Functions
Included with these changes are a set of templates for TypeScript, type definitions, and npm scripts. Read on to learn more details about...
Read more >TypeScript support, Media library folders available in Strapi v4.3
Cover image for TypeScript support, Media library folders available ... Write your Strapi application code or develop plugins in TypeScript ...
Read more >Introducing TypeScript support for building AWS ...
In this blog post, we showed you how to develop, test, and register a resource type authored in TypeScript. The CloudFormation CLI and...
Read more >Documentation - TypeScript 3.9
While there's still room for improvement, we hope this work translates to a ... Visual Studio Code supports selecting different versions of TypeScript....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Many thanks for your suggestion @fabb! I refactored my media templates as you suggest and all typescript issues were gone, plus I got better autoformatting by Prettier in a couple of places 🎉
Wondering if docs could be changed to this:
css`...`
are removed 🎉@probablyup do you think this would make sense? I can submit a PR if that’s potentially interesting.
Imho writing media templates as template functions is not a good idea, because linting, syntax highlighting and auto completion won‘t work with its content css.
It is way eqsier and imho better to just create strings with the
@media
definitions:The media definitions could also read from the theme, this would work similarly.