Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 css modules 的类型问题 #35

Open
sorrycc opened this issue Nov 13, 2024 · 0 comments
Open

优化 css modules 的类型问题 #35

sorrycc opened this issue Nov 13, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@sorrycc
Copy link
Member

sorrycc commented Nov 13, 2024

Problem

目前 css modules 的类型处理是生成这样的提示,代码见 https://github.com/umijs/tnf/blob/b34554b/src/sync/write_types.ts#L67-L78

declare module '*.less' {
  const classes: { [key: string]: string };
  export default classes;
}
declare module '*.css' {
  const classes: { [key: string]: string };
  export default classes;
}

但是,在使用 css modules 时,比如

import styles from 'foo.module.css';

styles.xxx 时会没有具体 key 的提示。

参考:
https://github.com/mrmckeb/typescript-plugin-css-modules

Solution

1、使用 typescript-plugin-css-modules
2、优先尝试在 write_types.ts 里自动加此 plugin,让用户无需感知
3、如果不行,在脚手架里添加依赖和对应的 compilerOptions.plugins

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-plugin-css-modules"
      }
    ]
  }
}

Alternatives

typed-css-modules ,但缺点是会生成一堆 .d.ts 文件。

参考:
https://github.com/Quramy/typed-css-modules

Importance

would make my life easier

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant