Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@thelabnyc/typed-scss-modules

thelabnyc735MIT8.2.2TypeScript support: included

TypeScript type definition generator for SCSS CSS Modules

scss, css modules, cli, typescript, type generator, scss modules

readme

@thelabnyc/typed-scss-modules

npm version

Generate TypeScript definitions (.d.ts) files for CSS Modules that are written in SCSS (.scss). Check out this post to learn more about the rationale and inspiration behind this package.

Example

For example, given the following SCSS:

@import "variables";

.text {
    color: $blue;

    &-highlighted {
        color: $yellow;
    }
}

The following type definitions will be generated:

export declare const text: string;
export declare const textHighlighted: string;

Basic Usage

Install & use:

npm install --save-dev @thelabnyc/typed-scss-modules
npx typed-scss-modules src

Or:

npx @thelabnyc/typed-scss-modules

CLI Options

For all possible commands, run typed-scss-modules --help.

The only required argument is the directory where all SCSS files are located. Running typed-scss-modules src will search for all files matching src/**/*.scss. This can be overridden by providing a glob pattern instead of a directory. For example, typed-scss-modules src/*.scss

--watch (-w)

  • Type: boolean
  • Default: false
  • Example: typed-scss-modules src --watch

Watch for files that get added or are changed and generate the corresponding type definitions.

--ignoreInitial

  • Type: boolean
  • Default: false
  • Example: typed-scss-modules src --watch --ignoreInitial

Skips the initial build when passing the watch flag. Use this when running concurrently with another watch, but the initial build should happen first. You would run without watch first, then start off the concurrent runs after.

--ignore

  • Type: string[]
  • Default: []
  • Example: typed-scss-modules src --watch --ignore "**/secret.scss"

A pattern or an array of glob patterns to exclude files that match and avoid generating type definitions.

--includePaths (-i)

  • Type: string[]
  • Default: []
  • Example: typed-scss-modules src --includePaths src/core

An array of paths to look in to attempt to resolve your @import declarations. This example will search the src/core directory when resolving imports.

--aliases (-a)

  • Type: object
  • Default: {}
  • Example: typed-scss-modules src --aliases.~some-alias src/core/variables

An object of aliases to map to their corresponding paths. This example will replace any @import '~alias' with @import 'src/core/variables'.

--aliasPrefixes (-p)

  • Type: object
  • Default: {}
  • Example: typed-scss-modules src --aliasPrefixes.~ node_modules/

An object of prefix strings to replace with their corresponding paths. This example will replace any @import '~bootstrap/lib/bootstrap' with @import 'node_modules/bootstrap/lib/bootstrap'. This matches the common use-case for importing scss files from node_modules when sass-loader will be used with webpack to compile the project.

--nameFormat (-n)

  • Type: "all" | "camel" | "kebab" | "param" | "snake" | "dashes" | "none"
  • Default: "camel"
  • Examples:
    • typed-scss-modules src --nameFormat camel
    • typed-scss-modules src --nameFormat kebab --nameFormat dashes --exportType default. In order to use multiple formatters, you must use --exportType default.

The class naming format to use when converting the classes to type definitions.

  • all: makes use of all formatters (except all and none) and converts all class names to their respective formats, with no duplication. In order to use this option, you must use --exportType default.
  • camel: convert all class names to camel-case, e.g. App-Logo => appLogo.
  • kebab/param: convert all class names to kebab/param case, e.g. App-Logo => app-logo (all lower case with '-' separators).
  • dashes: only convert class names containing dashes to camel-case, leave others alone, e.g. App => App, App-Logo => appLogo. Matches the webpack css-loader camelCase 'dashesOnly' option.
  • snake: convert all class names to lower case with underscores between words.
  • none: do not modify the given class names (you should use --exportType default when using --nameFormat none as any classes with a - in them are invalid as normal variable names). Note: If you are using create-react-app v2.x and have NOT ejected, --nameFormat none --exportType default matches the class names that are generated in CRA's webpack's config.

--listDifferent (-l)

  • Type: boolean
  • Default: false
  • Example: typed-scss-modules src --listDifferent

List any type definition files that are different than those that would be generated. If any are different, exit with a status code 1.

--exportType (-e)

  • Type: "named" | "default"
  • Default: "named"
  • Example: typed-scss-modules src --exportType default

The export type to use when generating type definitions.

named

Given the following SCSS:

.text {
    color: blue;

    &-highlighted {
        color: yellow;
    }
}

The following type definitions will be generated:

export declare const text: string;
export declare const textHighlighted: string;

default

Given the following SCSS:

.text {
    color: blue;

    &-highlighted {
        color: yellow;
    }
}

The following type definitions will be generated:

export type Styles = {
    text: string;
    textHighlighted: string;
};

export type ClassNames = keyof Styles;

declare const styles: Styles;

export default styles;

This export type is useful when using kebab (param) cased class names since variables with a - are not valid variables and will produce invalid types or when a class name is a TypeScript keyword (eg: while or delete). Additionally, the Styles and ClassNames types are exported which can be useful for properly typing variables, functions, etc. when working with dynamic class names.

--exportTypeName

  • Type: string
  • Default: "ClassNames"
  • Example: typed-scss-modules src --exportType default --exportTypeName ClassesType

Customize the type name exported in the generated file when --exportType is set to "default". Only default exports are affected by this command. This example will change the export type line to:

export type ClassesType = keyof Styles;

--exportTypeInterface

  • Type: string
  • Default: "Styles"
  • Example: typed-scss-modules src --exportType default --exportTypeInterface IStyles

Customize the interface name exported in the generated file when --exportType is set to "default". Only default exports are affected by this command. This example will change the export interface line to:

export type IStyles = {
    // ...
};

--quoteType (-q)

  • Type: "single" | "double"
  • Default: "single"
  • Example: typed-scss-modules src --exportType default --quoteType double

Specify a quote type to match your TypeScript configuration. Only default exports are affected by this command. This example will wrap class names with double quotes ("). If Prettier is installed and configured in the project, it will be used and is likely to override the effect of this setting.

--updateStaleOnly (-u)

  • Type: boolean
  • Default: false
  • Example: typed-scss-modules src --updateStaleOnly

Overwrite generated files only if the source file has more recent changes. This can be useful if you want to avoid extraneous file updates, which can cause watcher processes to trigger unnecessarily (e.g. tsc --watch). This is done by first checking if the generated file was modified more recently than the source file, and secondly by comparing the existing file contents to the generated file contents.

Caveat: If a generated type definition file is updated manually, it won't be re-generated until the corresponding scss file is also updated.

--logLevel (-L)

  • Type: "verbose" | "error" | "info" | "silent"
  • Default: "verbose"
  • Example: typed-scss-modules src --logLevel error

Sets verbosity level of console output.

verbose

Print all messages

error

Print only errors

info

Print only some messages

silent

Print nothing

--banner

  • Type: string
  • Default: undefined
  • Example: typed-scss-modules src --banner '// This is an example banner\n'

Will prepend a string to the top of your output files

// This is an example banner
export type Styles = {
    // ...
};

--outputFolder (-o)

  • Type: string
  • Default: none
  • Example: typed-scss-modules src --outputFolder __generated__

Set a relative folder to output the generated type definitions. Instead of writing the type definitions directly next to each SCSS module (sibling file), it will write to the output folder with the same path.

It will use the relative path to the SCSS module from where this tool is executed. This same path (including any directories) will be constructed in the output folder. This is important for this to work properly with TypeScript.

Important: for this to work as expected the tsconfig.json needs to have rootDirs added with the same output folder. This will allow TypeScript to pick up these type definitions and map them to the actual SCSS modules.

{
    "compilerOptions": {
        "rootDirs": [".", "__generated__"]
    }
}

--additionalData (-d)

  • Type: string
  • Default: none
  • Example: typed-scss-modules src --additionalData '$global-var: green;'

Prepend the provided SCSS code before each file. This is useful for injecting globals into every file, such as adding an import to load global variables for each file.

--allowArbitraryExtensions

  • Type: boolean
  • Default: false
  • Example: typed-scss-modules src --allowArbitraryExtensions

Output filenames that will be compatible with the "arbitrary file extensions" feature that was introduced in TypeScript 5.0. See the docs for more info.

In essence, the *.scss.d.ts extension now becomes *.d.scss.ts so that you can import SCSS modules in projects using ESM module resolution.

Config options

All options above are also supported as a configuration file in the root of the project. The following configuration file names are supported:

  • typed-scss-modules.config.ts
  • typed-scss-modules.config.js

The file can provide either a named config export or a default export.

// Example of a named export with some of the options sets.
export const config = {
    banner: "// customer banner",
    exportType: "default",
    exportTypeName: "TheClasses",
    logLevel: "error",
};

// Example of a default export with some of the options sets.
export default {
    banner: "// customer banner",
    exportType: "default",
    exportTypeName: "TheClasses",
    logLevel: "error",
};

Note: the configuration options are the same as the CLI options without the leading dashes (--). Only the full option name is supported (not aliases) in the configuration file.

CLI options will take precedence over configuration file options.

In addition to all CLI options, the following are options only available with the configuration file:

importers

  • Type: AnyImporter[]
  • Default: none

Define an array of custom SASS importers. This should only be necessary if custom SASS importers are already being used in the build process. This is used internally to implement aliases and aliasPrefixes.

Refer to lib/sass/importer.ts for more details and the Sass importer documentation.

Examples

For examples of how this tool can be used and configured, see the examples directory:

changelog

Changelog

v8.2.2 (2025-06-12)

Fix

  • restore glob functionality in file watch mode (#9)
  • broken dependency chain under yarn with PnP (#6)
  • enhance example test cases
  • prevent URL comparison false negatives

v8.2.1 (2025-06-11)

Fix

  • reflect includePaths into alias resolution
  • resolve importers interface mismatches

v8.2.0 (2025-05-29)

Fix

  • remove npmrc file now that @thelabnyc/standards is on NPM also
  • deps: remove @types/sass

v8.2.0-b0 (2025-05-29)

Feat

  • migrate all sync IO to async IO
  • deps: update changeCase
  • deps: update glob v7 -> v11

v8.2.0-a2 (2025-05-28)

v8.2.0-a1 (2025-05-28)

v8.2.0-a0 (2025-05-28)

Feat

  • migrate off of deprecated sass.render API
  • refactor as ESM

Fix

  • migrate deprecated Sass syntax in tests and examples

v8.1.1 (2025-01-11)

Fix

  • upgrade bundle require

v8.1.0 (2025-01-01)

Feat

  • add --allowArbitraryExtensions option (compatible with the TS 5.0 feature of the same name)

v8.0.1 (2024-03-23)

Fix

  • resolve Prettier config based on generated file

v8.0.0 (2023-11-25)

BREAKING CHANGE

  • upgrade minimum node version to 16

v7.1.4 (2023-08-06)

Fix

  • clear out old types for empty files

v7.1.3 (2023-08-05)

Fix

  • updateStaleOnly: compare file contents

v7.1.2 (2023-07-23)

Fix

  • list-different: respect ignore option

v7.1.1 (2023-06-18)

Fix

  • export the named types as declared

v7.1.0 (2023-03-14)

Feat

  • node-sass: support 8.x as a peer dependency

v7.0.2 (2023-01-10)

Fix

  • restore number camel-casing behavior changed in MR 161

v7.0.1 (2022-09-29)

Fix

  • handle single nameFormat without array literal

v7.0.0 (2022-09-14)

BREAKING CHANGE

  • Upgrade the minimum node-sass version to 7 to support latest the Node versions

v6.6.0 (2022-09-13)

Feat

  • perform sanity check in watch mode

v6.5.0 (2022-06-03)

Feat

  • file-to-class-names: adds support for multiple name formatters

Fix

  • improves even further by removing the unnecessary NameFormatInput type
  • improves types used to avoid casting
  • improve types to be more maintainable
  • refactor code to match expectations as nameFormat becomes an array

v6.4.1 (2022-05-10)

Fix

  • cli accepts 'snake' name format option

v6.4.0 (2022-05-01)

Feat

  • add snake class name transformer

Fix

  • add updated yarn.lock

v6.3.0 (2022-04-27)

Feat

  • export main as tsm

v6.2.1 (2022-04-25)

Fix

  • alias and aliasPrefixes in config file

v6.2.0 (2022-02-16)

Feat

  • add additionalData

v6.1.0 (2022-02-16)

Feat

  • add outputFolder

v6.0.0 (2022-02-14)

BREAKING CHANGE

  • sass updated to 1.49.7 and node-sass updated to 4.12.0

v5.1.1 (2022-02-13)

Fix

  • remove extra logging

v5.1.0 (2022-02-13)

Feat

  • add config file with custom importer support

Refactor

  • move defaults out so options properly merge

v5.0.0 (2022-01-31)

BREAKING CHANGE

  • this package's bin (CLI) name was changed from 'tsm' to 'typed-scss-modules'.

Fix

  • update bin

v4.1.3 (2022-01-31)

Fix

  • update bin

v4.1.2 (2022-01-02)

Fix

  • list-different: raise error if no type file

Refactor

  • list-diffrent: early return

v4.1.1 (2021-03-21)

Fix

  • banner: fix banner formatting

v4.1.0 (2021-02-21)

Feat

  • composition: fix unit tests
  • composition: path fetcher should return an object, not an array
  • composition: support for composes: class from 'filename'

v4.0.0 (2021-02-05)

BREAKING CHANGE

  • when using exportType default, the output will now have a semicolon after the type

Fix

  • formatting: add missing semicolon after type

v3.4.1 (2021-01-10)

Fix

  • fix --updateStaleOnly issue where new files cause crash

v3.4.0 (2020-11-19)

Feat

  • add update option to prevent unnecessary file updates

v3.3.0 (2020-10-27)

Feat

  • add ability to delete old .d.ts files for *.scss files

Fix

  • remove an extra message from alert

v3.2.2 (2020-10-14)

Fix

  • list-different: handle prettier formatting

v3.2.1 (2020-10-10)

Fix

  • addresses CRLF vs LF issue on windows

v3.2.0 (2020-10-07)

Feat

  • banner: example updated
  • banner-required: ensuring build works
  • banner: add documentation to README
  • banner: testing support

v3.1.0 (2020-09-14)

Feat

  • use prettier to format files before save if available

Refactor

  • use spread operator for new object

v3.0.0 (2020-08-25)

BREAKING CHANGE

  • re-releasing as a major since previous changes were breaking

Fix

  • docs: formatting

v2.1.0 (2020-08-25)

Feat

  • sort class names

v2.0.1 (2020-07-17)

Fix

  • docs: fix logLevel options alias

v2.0.0 (2020-07-06)

BREAKING CHANGE

  • this can interfere with how others use their default exported classnames

Fix

  • export-default: export a type instead of an interface Fixes #71

v1.4.0 (2020-07-02)

Feat

  • loglevel: implement logLevel option

Fix

  • prettier: readme formatting
  • loglevel: fix code according to PR comments
  • cli: fix option alias conflict

v1.3.0 (2020-04-07)

Feat

  • cli: add augmenting options exportTypeName/exportTypeInterface

v1.2.0 (2020-02-21)

Feat

  • quotetype: implement the quoteType option
  • quotetype: implement the quoteType option

v1.1.0 (2020-01-19)

Feat

  • sass: add dart-sass support

v1.0.1 (2019-12-08)

Fix

  • deps: upgrade deps and fix breaking change

v1.0.0 (2019-12-08)

Feat

  • releases: setup automated releases
  • ignore: implement the ignore option
  • option: allow to ignore initial build in watch
  • types: add Styles and ClassNames types when using default export
  • list-different: add list-different functionality
  • watch: add support for watching files
  • export-type: add support for default export types
  • cli: add -i as an alias for --includePaths
  • cli: add better example
  • directories: support directories as well as globs
  • cli: add local runner helper and better UX for CLI
  • glob: allow passing a glob pattern
  • writing: add basics for writing out type defs
  • casing: add support for transforming classnames
  • cli: add basic cli parsing

Fix

  • travis: node version
  • yarn: add missing yarn.lock entry
  • dependencies: move some dev deps to dependencies
  • types: add types for reserved-words package
  • fs: remove fs
  • classname: only pass the classname when invoking map
  • cli: fix CLI name, path and execution
  • glob: fix glob patterns and remove ignore

Refactor

  • main: move much of logic to isolated files in lib/core
  • sass: update the file parsing