🌐
vanilla-extract
vanilla-extract.style
vanilla-extract — Zero-runtime Stylesheets-in-TypeScript.
Use TypeScript as your preprocessor. Write type‑safe, locally scoped classes, variables and themes, then generate static CSS files at build time. $npm install @vanilla-extract/css · Get started · GitHub · styles.css.ts ·
🌐
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
🌐
GitHub
github.com › topics › stylesheet
stylesheet · GitHub Topics · GitHub
💅 Micro-library that writes stylesheets with a non-opinionated approach, free of dependencies, and in the easiest way possible. typescript react-native stylesheet micro-library writing-stylesheets
🌐
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%
🌐
GitHub
github.com › thoughtbot › react-native-typescript-styles
GitHub - thoughtbot/react-native-typescript-styles: Kick-start your new React Native project with simple, organized styles.
September 24, 2019 - const style = StyleSheet.create({ header: { paddingBottom: Sizing.x20, borderBottomWidth: Outlines.borderWidth.thin, borderColor: Colors.neutral.gray6, }, headerText: { ...Typography.bold.x50, }, button: { ...Buttons.bar.primary, }, buttonText: { ...Buttons.barText.primary, }, });
Starred by 19 users
Forked by 3 users
Languages   TypeScript 95.3% | JavaScript 4.7% | TypeScript 95.3% | JavaScript 4.7%
🌐
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.
🌐
Typhonjs-typedoc
typhonjs-typedoc.github.io › ts-lib-docs › 2023 › dom › interfaces › StyleSheet.html
StyleSheet | Typescript Library Declarations (DOM)
Typescript Library Declarations (DOM) A single style sheet. CSS style sheets will further implement the more specialized CSSStyleSheet interface. MDN Reference · StyleSheet · disabled href media owner · Node parent · Style · Sheet title type · disabled: boolean ·
🌐
GitHub
github.com › microsoft › TypeScript-React-Native-Starter › issues › 25
no code hint to use StyleSheet.create · Issue #25 · microsoft/TypeScript-React-Native-Starter
January 30, 2018 - There is no code hint to use this method(use webstrom ide) const styles = StyleSheet.create({ root: { alignItems: "center", alignSelf: "center" } }); hope when write 'a', a code prompt appears. [alignSelf....all start with 'a' ]
Author   Joey-Huang
🌐
npm
npmjs.com › package › typescript-style-sheet
typescript-style-sheet - npm
November 13, 2018 - npm i typescript-style-sheet · github.com/sammi/typescript-style-sheet · github.com/sammi/typescript-style-sheet#readme · 2 · 0.0.4 · MIT · 2.06 kB · 2 · 6 years ago · Try on RunKit ·
      » npm install typescript-style-sheet
    
Published   Nov 13, 2018
Version   0.0.4
Author   Sammi Song
Find elsewhere
🌐
GitHub
github.com › Quramy › typed-css-modules
GitHub - Quramy/typed-css-modules: Creates .d.ts files from CSS Modules .css files · GitHub
Creates TypeScript definition files from CSS Modules .css files.
Starred by 1.1K users
Forked by 68 users
Languages   TypeScript 80.4% | JavaScript 16.8% | CSS 2.3%
🌐
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 - Note: To make typescript-plugin-css-modules working you should set VSC to use Workspace version of Type Script as described in plugin manual https://github.com/mrmckeb/typescript-plugin-css-modules#visual-studio-code
🌐
Detox
wix.github.io › stylable
Welcome to Stylable - CSS for Components
We want to give CSS a type system — to do for CSS what TypeScript does for JavaScript.
🌐
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 type support from the editor, and then pass it to a css function, or a styled component.
Author   carpben
🌐
Typestyle
typestyle.github.io
TypeStyle
We cannot provide a description for this page right now
🌐
GitHub
github.com › frenic › csstype
GitHub - frenic/csstype: Strict TypeScript and Flow types for style based on MDN data · GitHub
TypeScript and Flow definitions for CSS, generated by data from MDN.
Starred by 1.8K users
Forked by 75 users
Languages   TypeScript 98.4% | JavaScript 1.6%
🌐
Carl Rippon
carlrippon.com › using-css-react-typescript-with-webpack5
Using CSS in React and TypeScript with Webpack 5
That’s it! Our React and TypeScript project is now set up to use CSS. This code is available in GitHub at https://github.com/carlrip/react-typescript-css-webpack.
🌐
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › discussions › 62481
[@types/react-native] StyleSheet.create won't raise error for unexisting properties · DefinitelyTyped/DefinitelyTyped · Discussion #62481
Basically, TypeScript won't raise any Error for the object passed into StyleSheet.create() with any kind of unexisting properties. But I have totally no idea why. I didn't find anything like [K: st...
Author   DefinitelyTyped
🌐
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.
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.