StylesSheet doesn't support using fuctions but you can use an Object or a Function instead and type the return value with one of these types ViewStyle | TextStyle | ImageStyle

E.g

const styles = {
    square: (size: number): ViewStyle => ({
        width: size,
        height: size,
    })
};
Answer from Daniel Gabor on Stack Overflow
🌐
Reddit
reddit.com › r/reactnative › how can i get stylesheet types (typescript) without using stylesheet.create()
r/reactnative on Reddit: How can I get StyleSheet Types (TypeScript) without using StyleSheet.create()
May 21, 2021 -
const styleSheet: MagicalTypes = {
  container: {
    borderColor: 'red',
    borderWidth: 2,
    padding: 5,
  },
  textStyle: {
    fontSize: 16,
  },
};

The reason that I don't wanna use StyleSheet.create right now is because I want to do some manipulation before creating.

MagicalTypes is probably an interface (I imagine the solution is an interface) that I need to build myself, but I couldn't figure out how. It should be dynamic (have a different number of children every time).

I saw that there is StyleSheet.NamedStyles<> but I couldn't figure out a way to implement in this interface.

Any idea?

🌐
Oida
oida.dev › typescript-react › styles
TypeScript and React: Styles and CSS - oida.dev
July 17, 2019 - Emotion – the one with the David Bowie Emoji 👩‍🎤 – is a pretty nice framework with lots of ways to add styles to your components. They also have a very good TypeScript guide.
Discussions

Using functions within React Native StyleSheet with Typescript - Stack Overflow
I don't think StyleSheet supports having functions in it's keys. It is meant to act something like stylesheets from the web. I suspect that what you were doing before was unsupported but now that you're using TypeScript, it's enforcing this. More on stackoverflow.com
🌐 stackoverflow.com
Using a function on stylesheet typescript react native - Stack Overflow
I try to use some variables inside my StyleSheet file. The UI is working fine. But the type is error. Any idea how to solve this? type buttonStyle = (height: number, width: string, color: string) =... More on stackoverflow.com
🌐 stackoverflow.com
How to include .css file in .tsx typescript?
For example using webpack, you ... and add it to the build before safely compiling your typescript. If you also have webpack generate your HTML then your CSS will be automatically injected as a stylesheet.... More on stackoverflow.com
🌐 stackoverflow.com
Typescript Type support for style objects
I'm working with React Typescript and Emotion. So far I have used template strings, but I'd like to gradually move to style objects. I'd like to be able to declare a style object, get t... More on github.com
🌐 github.com
6
March 12, 2019
🌐
vanilla-extract
vanilla-extract.style
vanilla-extract — Zero-runtime Stylesheets-in-TypeScript.
All styles generated at build time — just like Sass, LESS, etc, but with the power of TypeScript.
🌐
Fettblog
fettblog.eu › typescript-react › styles
TypeScript and React: Styles and CSS
Emotion – the one with the David Bowie Emoji 👩‍🎤 – is a pretty nice framework with lots of ways to add styles to your components. They also have a very good TypeScript guide.
🌐
GitHub
github.com › thoughtbot › react-native-typescript-styles › blob › main › STYLE_GUIDE.md
react-native-typescript-styles/STYLE_GUIDE.md at main · thoughtbot/react-native-typescript-styles
// index.ts import * as Buttons from "./buttons" import * as Colors from "./colors" import * as Sizing from "./sizing" import * as Typography from "./typography" export { Buttons, Colors, Sizing, Typography } // component.ts import { Sizing, Colors } from "../styles" ... const style = StyleSheet.create({ container: { padding: Sizing.layout.x10 backgroundColor: Colors.neutral.gray6 } })
Author   thoughtbot
🌐
Medium
medium.com › @hakoniemi › react-native-and-typescript-meets-styles-b727ecf7e677
React Native and TypeScript meets Styles | by Samuli Hakoniemi | Medium
January 24, 2018 - import { StyleSheet, Text, TextStyle, View, ViewStyle } from ‘react-native’; After this we are good to go for defining an interface: interface Styles { wrapper: ViewStyle; textContent: TextStyle; } Because interfaces are cool and they keep our code consistent. And then we have the power of TypeScript to tell us what we are doing wrong (again).
Find elsewhere
🌐
npm
npmjs.com › package › typescript-style-sheet
typescript-style-sheet - npm
November 13, 2018 - Define CSS Style by TypeScript. Latest version: 0.0.4, last published: 6 years ago. Start using typescript-style-sheet in your project by running `npm i typescript-style-sheet`. There are no other projects in the npm registry using ...
      » npm install typescript-style-sheet
    
Published   Nov 13, 2018
Version   0.0.4
Author   Sammi Song
🌐
Stack Overflow
stackoverflow.com › questions › 71644471 › using-a-function-on-stylesheet-typescript-react-native
Using a function on stylesheet typescript react native - Stack Overflow
I try to use some variables inside my StyleSheet file. The UI is working fine. But the type is error. Any idea how to solve this? type buttonStyle = (height: number, width: string, color: string) =...
🌐
Medium
medium.com › @zvona › stylesheets-in-react-native-with-typescript-revisited-6b4ba0a899d2
Stylesheets in React Native with TypeScript Revisited | by Samuli Hakoniemi | Medium
December 23, 2020 - TypeScript compiler (and IDE with correct setup) is aware what kind of style properties Button component actually accepts.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-stylesheet
Mastering TypeScript Style Sheets: A Comprehensive Guide
TypeScript style sheets are a feature that allows you to write CSS-like code within your TypeScript files, while enjoying the benefits of type safety and IntelliSense support.
🌐
GitHub
github.com › typestyle › typestyle
GitHub - typestyle/typestyle: Making CSS Typesafe 🌹
Use it like you would use CSS modules or CSS in general with webpack etc, but this time you get to use TypeScript / JavaScript!
Starred by 3.1K users
Forked by 86 users
Languages   TypeScript 96.9% | JavaScript 1.7% | HTML 1.4% | TypeScript 96.9% | JavaScript 1.7% | HTML 1.4%
🌐
CSS-Tricks
css-tricks.com › css-in-typescript-with-vanilla-extract
CSS in TypeScript with vanilla-extract | CSS-Tricks
October 7, 2021 - So, instead of injecting styles at runtime, vanilla-extract takes after Linaria and astroturf. These libraries let you author styles using JavaScript functions that get ripped out at build time and used to construct a CSS file. Although you write vanilla-extract in TypeScript, it doesn’t affect the overall size of your production JavaScript bundle.
🌐
xjavascript
xjavascript.com › blog › stylesheetcreate-typescript
Mastering `StyleSheet.create` in TypeScript — xjavascript.com
In React Native, StyleSheet.create is a utility function that allows you to define styles in a more organized and performant way. It takes an object where the keys are style names and the values are objects containing style properties.
🌐
GitHub
github.com › emotion-js › emotion › issues › 1267
Typescript Type support for style objects · Issue #1267 · emotion-js/emotion
March 12, 2019 - I'm working with React Typescript and Emotion. So far I have used template strings, but I'd like to gradually move to style objects. I'd like to be able to declare a style object, get t...
Author   carpben
🌐
Medium
medium.com › @dimi_2011 › setting-up-css-modules-in-typescript-project-52596526d19
Setting up CSS Modules in Typescript project | by Ivan Dimitrijevic | Medium
March 21, 2023 - Setting up CSS Modules in Typescript project To be honest I did not expect any problem with this topic but I have to say that I spent a day finding way how to setup CSS modules in typescript project …
🌐
React Native
reactnative.dev › docs › stylesheet
StyleSheet · React Native
5 days ago - An identity function for creating styles. The main practical benefit of creating styles inside StyleSheet.create() is static type checking against native style properties.
🌐
Google
google.github.io › styleguide › tsguide.html
Google TypeScript Style Guide
This guide is based on the internal Google TypeScript style guide, but it has been slightly adjusted to remove Google-internal sections. Google's internal environment has different constraints on TypeScript than you might find outside of Google.
Top answer
1 of 12
111

Now in the year 2021, all you need to do is add a src/Globals.d.ts to your project with these lines:

declare module "*.module.css";
declare module "*.module.scss";
// and so on for whatever flavor of css you're using

Then install and add

{
  "compilerOptions": {
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  }
}

to your tsconfig.

Example of this correctly functioning in VS code after making that simple change (root is a class defined in my stylesheet):

Webpack and tsc also compile correctly on the command line.

2 of 12
62

A) As you are saying, there is one simplest (not best) option to use require:

const css = require('./component.css')
  • We need to have typings for require as it's not standard feature in typescript.
  • Simplest typing for this specific require may be:

    declare function require(name: string): string;
    
  • Webpack will then compile typescript and use modules properly - BUT without any IDE help and class names checks for build.

B) There is better solution to use standard import:

import * as css from './component.css'
  • enables full class names IntelliSense
  • requires types definition for each css file, otherwise tsc compiler will fail

For proper IntelliSense, Webpack needs to generate types definition for each css file:

  1. Use webpack typings-for-css-modules-loader

    webpackConfig.module.loaders: [
      { test: /\.css$/, loader: 'typings-for-css-modules?modules' }
      { test: /\.scss$/, loader: 'typings-for-css-modules?modules&sass' }
    ];
    
  2. Loader will generate *.css.d.ts files for each of css files in your codebase

  3. Typescript compiler will understand that css import will be module with properties (class names) of type string.

Mentioned typings-for-css-loader contains a bug and because of types file generation delay, it's best to declare global *.css type in case our *.css.d.ts file is not generated yet.

That little bug scenario:

  1. Create css file component.css
  2. Include it in component import * as css from './component.css'
  3. Run webpack
  4. Typescript compiler will try to compile code (ERROR)
  5. Loader will generate Css modules typings file (component.css.d.ts), but it's late for typescript compiler to find new typings file
  6. Run webpack again will fix build error.

Easy fix is to create global definition (eg. file called typings.d.ts in your source root) for importing CSS Modules:

declare module '*.css' {
  interface IClassNames {
    [className: string]: string
  }
  const classNames: IClassNames;
  export = classNames;
}

This definition will be used if there is no css file generated (eg. you have added new css file). Otherwise will be used generated specific (needs to be in same folder and named same as source file + .d.ts extension), eg. component.css.d.ts definition and IntelliSense will work perfectly.

Example of component.css.d.ts:

export const wrapper: string;
export const button: string;
export const link: string;

And if you don't want to see generated css typings you may setup filter in IDE to hide all files with extension .css.d.ts in your sources.