Skip to content

Instantly share code, notes, and snippets.

@bezawislak
Last active July 25, 2016 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bezawislak/24026f8130a0fdee7a9d83117023c695 to your computer and use it in GitHub Desktop.
Save bezawislak/24026f8130a0fdee7a9d83117023c695 to your computer and use it in GitHub Desktop.
React CSSModules typings
declare module 'react-css-modules' {
interface Options {
allowMultiple?: boolean;
errorWhenNotFound?: boolean;
}
module Decorator {
interface Props {
styles?: any;
styleName?: string;
}
}
interface Decorator {
(defaultStyles: any, options?: Options): any;
}
const Decorator: Decorator;
export = Decorator;
}
declare module 'react-css-modules' {
interface Options {
allowMultiple?: boolean;
errorWhenNotFound?: boolean;
}
module CSSModules {
interface Props {
styles?: any;
styleName?: string;
}
}
interface CSSModules {
(defaultStyles: any, options?: Options): any;
}
const CSSModules: CSSModules;
export = CSSModules;
}
@empz
Copy link

empz commented Jul 2, 2016

I've added the react-css-modules.d.ts code to my custom d.ts file.

Then in my component I do:

import * as CSSModules from "react-css-modules";
const styles = require("./MasonryMessage.scss");

@CSSModules(styles)
export default class extends React.Component<MyProps, void> {
  public render() {
  }
}

The TypeScript compiler is not complaining but I'm getting an error in the browser that says TypeError: CSSModules is not a function

Any idea?

@empz
Copy link

empz commented Jul 2, 2016

For some reason it's not importing it properly. The actual function is in CSSModules.default so I have to do something like this which is kind of ugly:

import * as CSSModules from "react-css-modules";
const CSSModulesDecorator = (CSSModules as any).default;
const styles = require("./MasonryMessage.scss");

@CSSModulesDecorator(styles)
export default class extends React.Component<MyProps, void> {
  public render() {
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment