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

Question: use generated TypeScript declaration in my React component. #27

Open
sergiop opened this issue Oct 3, 2019 · 2 comments
Open

Comments

@sergiop
Copy link

sergiop commented Oct 3, 2019

Hi, I'm a Typescript newbie and I have a question not strictly related to this project: I used css-modules-typescript-loader to generate TypeScript declarations:

I have got this structure:

src/Components/MyComponent
├── MyComponent.module.css        // Css modules styles
├── MyComponent.module.css.d.ts   // Autogenerated style type declarations
└── MyComponent.tsx               // The React component

Inside MyComponent.tsx I have a function myFunc passing as a parameter styles object, in this way:

import React from 'react'
import styles from './MyComponent.module.css'

const myFunc = (styles, otherParam) => {...}

const MyComponent: React.FunctionComponent<ButtonProps> = (props) => {...}

Is it possible to import Interface from MyComponent.module.css.d.ts adding types like:

import React from 'react'
import styles from './MyComponent.module.css'

const myFunc = (styles: CssModuleInterface, otherParam: string): string => {...}

const MyComponent: React.FunctionComponent<ButtonProps> = (props) => {...}

How can I do it?

@mattpocock
Copy link

You can do typeof styles instead of CssModuleInterface.

@RichardTMiles
Copy link

RichardTMiles commented Aug 24, 2022

import styles from "./style.module.scss"

export function getStyles<iCSS extends {}>(overrides: iCSS = {} as iCSS) : iCSS & typeof styles {
    return {
        ...styles,
        ...overrides
    }
}

Works perfectly. Thanks!

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

No branches or pull requests

3 participants