npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-css-interfaces

v1.3.2

Published

Creates .d.ts files with interface definitions from .css files

Downloads

9,786

Readme

typed-css-interfaces

Creates TypeScript definition files with interfaces from .css files.

This is a fork of typed-css-modules with added interface support.

Given the following CSS:

/* styles.css */
@value primary: red;
.myClass {
  color: primary;
}

The original project generated the following output:

export const primary: string;
export const myClass: string;

So you could do:

/* app.ts */
import * as styles from './styles.css';
console.log(`<div class="${styles.myClass}"></div>`);
console.log(`<div style="color: ${styles.primary}"></div>`);

But, if you wanted to dynamically assign styles, like so:

let var = 'style-' + name;
console.log(`<div class="${styles[var]}"></div>`);

You will get a TS error about no index signature.

To get around this, this project generates the following output:

/* styles.css.d.ts */
interface Styles {
[name:string]:string;
primary: string;
myClass: string;
}
declare var styles:Styles;
export styles;

The [name:string]: string is the important part - it defines an index signature that allows Typescript to validate dynamically declared styles.

Invalid and Reserved Names

These are now quoted and added to the interface block, rather than ignored.
For example, .MyClass--Modifier will be added as '.MyClass--Modifier'

CLI

npm install -g typed-css-interfaces

To maintain backwards compatibility with typed-css-modules, this project uses the command tcmi

Exec tcmi <input directory>.

For example, if you have .css files under src directory, exec the following:

tcmi src

Then, this creates *.css.d.ts files under the directory which has original .css file.

(your project root)
- src/
    | myStyle.css
    | myStyle.css.d.ts [created]

output directory

Use -o or --outDir option.

For example:

tcmi -o dist src
(your project root)
- src/
    | myStyle.css
- dist/
    | myStyle.css.d.ts [created]

file name pattern

By the default, this tool searches **/*.css files under <input directory>.

If you can customize glob pattern, you can use --pattern or -p option.

tcmi -p src/**/*.icss

watch

With -w or --watch, this CLI watches files in the input directory.

camelize CSS token

With -c or --camelCase, kebab-cased CSS classes(such as .my-class {...}) are exported as camelized TypeScript varibale name(export const myClass: string).

You can pass --camelCase dashes to only camelize dashes in the class name. Since version 0.27.1 in the webpack css-loader. This will keep upperCase class names intact, e.g.:

.SomeComponent {
  height: 10px;
}

becomes

interface Styles {
SomeComponent: string;
}
declare var styles;
export = styles;

See also webpack css-loader's camelCase option.

API

No changes from the original project

Example

There is a minimum example in this repository example folder. Clone this repository and run cd example; npm i; npm start.

Or please see https://github.com/Quramy/typescript-css-modules-demo. It's a working demonstration of CSS Modules with React and TypeScript.

License

This software is released under the MIT License, see LICENSE.txt.