# typed-css-interfaces

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

Latest version **1.3.2** (published 2018-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install typed-css-interfaces
pnpm add typed-css-interfaces
yarn add typed-css-interfaces
bun add typed-css-interfaces
```

Provides the command `tcmi`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2018-09-07 |
| First published | 2017-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 267.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | quramy |
| Maintainers | samwaters |
| Keywords | css-modules, typescript |

## Links

- npm: https://www.npmjs.com/package/typed-css-interfaces
- Repository: https://github.com/samwaters/typed-css-modules
- Homepage: https://github.com/samwaters/typed-css-modules#readme
- Issues: https://github.com/samwaters/typed-css-modules/issues
- npm.io page: https://npm.io/package/typed-css-interfaces

## Dependencies (8)

- [glob](https://npm.io/package/glob.md) ^7.1.2
- [chalk](https://npm.io/package/chalk.md) ^2.1.0
- [yargs](https://npm.io/package/yargs.md) ^8.0.2
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [chokidar](https://npm.io/package/chokidar.md) ^1.6.1
- [is-there](https://npm.io/package/is-there.md) ^4.4.2
- [camelcase](https://npm.io/package/camelcase.md) ^4.1.0
- [css-modules-loader-core](https://npm.io/package/css-modules-loader-core.md) ^1.1.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.3.2 (latest) — 2018-09-07
- 1.3.1 — 2018-09-07
- 1.3.0 — 2018-05-01
- 1.2.1 — 2018-05-01
- 1.2.0 — 2018-05-01
- 1.1.0 — 2018-05-01
- 1.0.0 — 2017-09-22
- 0.0.7 — 2017-09-22
- 0.0.6 — 2017-09-22
- 0.0.5 — 2017-09-22
- 0.0.4 — 2017-09-22
- 0.0.3 — 2017-09-22
- 0.0.2 — 2017-09-22
- 0.0.1 — 2017-09-22

## 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:
```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:
```ts
/* 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:

```ts
/* 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

```sh
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:

```sh
tcmi src
```

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

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

#### output directory
Use `-o` or `--outDir` option.

For example:

```sh
tcmi -o dist src
```

```text
(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.

```sh
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.:

```css
.SomeComponent {
  height: 10px;
}
```

becomes

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

See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).

## 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](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.

---
_Source: https://npm.io/package/typed-css-interfaces · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
