Skip to content

styles become any type when import specifier is used #133

@mizdra

Description

@mizdra

css-modules-kit uses paths to resolve import specifiers.

Image

However, type checking of type definition files generated by codegen does not work properly.

// src/a.module.css
@import '@/d.module.css';
// src/d.module.css
.d_1 { color: red; }
// generated/a.module.css.d.ts
// @ts-nocheck
declare const styles = {
  ...(await import('@/src/d.module.css')).default,
  //                ^^^^^^^^^^^^^^^^^^
  //                This import speficier cannot be resolved by tsc.
  //                Therefore, the `(await import(...)) .default)` will be `any` type.
  //                Worse, `styles` will also be of `any` type.
};
export default styles;

Since styles will be any type, a type error will not be reported if you reference a non-existent class name from *.ts.

// src/a.ts
import styles from './a.module.css';
styles.unknown; // This should be reported as a type error
$ npx tsc
$ echo $?
0

Additional Information

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions