score:13
import
has special meaning to TypeScript. It means that TypeScript will attempt to load and understand the thing being imported. The right way is to define require
like you mentioned but then var
instead of import
:
var styles = require('../../../css/tree.css')`
score:0
A bit late to game but you can create a file called tree.css.d.ts in the same folder as tree.css that has this line:
export const h3: string;
and still use the import statement import * as styles from ...
and you will still getcode completion and compile time checking.
You can either manage these definition files manually or you could integrate typed-css-modules into your build pipeline (https://github.com/Quramy/typed-css-modules)
score:2
I had similar problem. For me, works import:
import '../../../css/tree.css';
Webpack change this like any other normal imports. It change it to
__webpack_require__(id)
One drawback is that you lost control on style variable.
score:2
You can use https://github.com/Quramy/typed-css-modules, which creates .d.ts files from CSS Modules .css files. Please see also https://github.com/css-modules/css-modules/issues/61#issuecomment-220684795
score:9
- Declare 'require' as per ts-loader documentation.
- Use 'require' as generic with < any > type: require< any >("../../../css/tree.css").
*.d.ts file
declare var require: {
<T>(path: string): T;
(paths: string[], callback: (...modules: any[]) => void): void;
ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void) => void;
};
*.tsx file with component
const styles = require<any>("../../../css/tree.css");
...
<h3 className={styles.h3}>Components</h3>
I know it was already answered, but I was struggling with it for a while before I realized I need to use generic type specification, without that I wasn't able to access content of CSS file. (I was getting error: Property 'h3' does not exists on type '{}'.)
Source: stackoverflow.com
Related Query
- Use CSS Modules in React components with Typescript built by webpack
- Searching for Webpack config for React Components Library with Typescript, SASS & CSS Modules via SASS support
- Error using css modules in typescript react with webpack config
- How to use CSS Modules with webpack in React isomorphic app?
- How to import CSS modules with Typescript, React and Webpack
- How to style child components in React with CSS Modules
- Testing Webpack built React components with Jest
- Use PrimeReact Themes with CSS Modules Enabled in React Application
- Is it possible to use MaterialUI with React and css modules and access the theme inside the css module file?
- React built with Webpack 5 typescript compiled with problems in browser. @tailwind directive not working postcss-loader postcss.config.js Emit skipped
- React - Use Bootstrap globally with css modules
- How to use @apply in a react component with css modules
- What's best way to config React components library with Webpack, typescript, css modules and SASS
- Test react components with jest and enzyme and css modules
- How can you use CSS modules together with Typescript, React and Webpack?
- How do i use different css files for different components React with same class names
- react app with webpack built from scratch is not loading an image in css
- How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
- Configuring CSS modules in development/production with webpack and React
- How to use scss with css modules / postcss inside components
- How to use nested css class (cascade) with React CSS Modules
- Webpack Dev Server cannot find modules with Typescript if I use custom paths in tsconfig
- How to use code splitting with webpack in react app with components exported as export * from '...'
- Can i use Enzyme's .hasClass feature with react components importing css files?
- How to use refs in React with Typescript
- How to apply global styles with CSS modules in a react app?
- How to use images in css with Webpack
- How to use css modules with create-react-app?
- Multiple classNames with CSS Modules and React
- Sass with CSS Modules & Webpack
More Query from same tag
- Manipulate HTML5 dialog in React
- How to make Login using redux in react js
- React js navigator implemented in web app
- React load script on specfic div
- ReactJS Material UI how to prevent Autocomplete to change the value
- how can i add object in array which is in object as property (react.js )
- How to write a Typescript React Component Wrapper
- Call an internal function in React component
- Node.JS build broken on Azure DevOps with Windows image
- React fetch api keep requesting on loop
- Element values shifting and changing before transition with react-transition-group
- React strap Cards unable to align items according to the screen size
- Nested routes in react-router
- <input> value prop is applied without re-rendering
- React Router Link doesn't work with LeafletJS
- Getting Project Version from Node / package.json in Jenkins
- useState state variable not updating
- Proper helper or custom React hook to add and initialise Firebase
- Css body width changes between desktop and mobile
- What's the idiomatic way to pass component prop to a component in Vue.js?
- How the input to store enhancer is provided in ReactJs
- Button onclick does not working in reactJS with below code
- React destructuring state to pass into component props
- loop adding component in reactjs
- Conditionally set attributes in styled-component using props to index on theme object with TypeScript?
- Setting iframe height to scrollHeight in GatsbyJS
- React: What does following mean: import register, * as fromRegister from './registerReducer'?
- Uncaught TypeError: Failed to execute 'selectNode' on 'Range': parameter 1 is not of type 'Node'
- Unable to filter out redux state based on values from another local state
- How can I use react-perfect-scrollbar on react-table to override default scrollbar?