
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
friendly-typed-css-modules
Advanced tools
This is a fork of Typed-CSS-Modules, aimed at providing some friendlier functionality for using programmatic class names against your typed css, as well as scss parsing support.
Creates TypeScript definition files from CSS Modules .css files.
If you have the following css,
/* styles.css */
@value primary: red;
.myClass {
color: primary;
}
typed-css-modules creates the following .d.ts files from the above css:
/* styles.css.d.ts */
interface Styles {
myClass: string,
}
const styles: Styles;
export = styles;
So, you can import CSS modules' class or variable into your TypeScript sources:
/* app.ts */
import * as styles from './styles.css';
console.log(`<div class="${styles.myClass}"></div>`);
console.log(`<div style="color: ${styles.primary}"></div>`);
You can also still programmatically generate accessors without getting errors.
console.log(`<div class="${styles[doSomethingProgrammatic()]}"></div>`);
npm install -g friendly-typed-css-modules
And exec ftcm <input directory>
command.
For example, if you have .css files under src
directory, exec the following:
ftcm src
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]
Use -o
or --outDir
option.
For example:
ftcm -o dist src
(your project root)
- src/
| myStyle.css
- dist/
| myStyle.css.d.ts [created]
By the default, this tool searches **/*.css
files under <input directory>
.
If you need to customize glob pattern, you use --pattern
or -p
option.
ftcm -p src/**/*.icss
With -w
or --watch
, this CLI watches files in the input directory.
With -c
or --camelCase
, kebab-cased CSS classes(such as .my-class {...}
) are exported as camelized TypeScript varibale name(export const myClass: string
).
See also webpack css-loader's camelCase option.
npm install typed-css-modules
import DtsCreator from 'typed-css-modules';
let creator = new DtsCreator();
creator.create('src/style.css').then(content => {
console.log(content.tokens); // ['myClass']
console.log(content.formatted); // 'class Styles...'
content.writeFile(); // writes this content to "src/style.css.d.ts"
});
DtsCreator instances process CSS and create TypeScript definitions.
new DtsCreator(option)
You can set the following options:
option.rootDir
: Project root directory(default: process.cwd()
).option.searchDir
: Directory which includes target *.css
files(default: './'
).option.outDir
: Output directory(default: option.searchDir
).option.camelCase
: Camelize CSS class tokens.option.allowGenericStringAccess
: Allow access to the styles object via generic string accessors (eg. styles[someVariable];
)create(filepath, contents) => Promise(dtsContent)
Returns DtsContent
instance.
filepath
: path of target .css file.contents
(optional): the CSS content of the filepath
. If set, DtsCreator uses the contents instead of the original contents of the filepath
.DtsContent instances contain *.d.ts
content, the final output path, and a function to write to file.
writeFile() => Promise(dtsContent)
Writes the DtsContent instance's content to a file.
dtsContent
: the DtsContent instance itself.tokens
An array of tokens retrieved from input CSS file.
e.g. ['myClass']
contents
An array of members of the styles class for this module.
e.g. ['myClass: string;']
.
formatted
A string containing TypeScript definitions.
e.g.
export const myClass: string;
messageList
An array of messages containing invalid token information.
e.g. ['my-class is not valid TypeScript variable name.']
.
outputFilePath
Final output file path.
If your input CSS file has any of the following class names, these invalid tokens are not output to .d.ts
files.
/* TypeScript reserved word */
.while {
color: red;
}
/* invalid TypeScript variable */
/* If camelCase option is set, this token will be converted to 'myClass' */
.my-class{
color: red;
}
/* it's ok */
.myClass {
color: red;
}
There is a minimum example in this repository example
folder. Clone this repository and run cd example; npm i; npm start
.
This software is released under the MIT License, see LICENSE.txt.
FAQs
Creates .d.ts files from CSS Modules .css files
The npm package friendly-typed-css-modules receives a total of 15 weekly downloads. As such, friendly-typed-css-modules popularity was classified as not popular.
We found that friendly-typed-css-modules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.