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

How to declare module type for Typescript? #19

Closed
BoldyrevDA opened this issue Nov 17, 2023 · 2 comments
Closed

How to declare module type for Typescript? #19

BoldyrevDA opened this issue Nov 17, 2023 · 2 comments

Comments

@BoldyrevDA
Copy link

There are different ways to import css by rollup-plugin-import-css.

So, this way

import styles from "./styles.css"; /* import the styles as a string */

I described in declaration.d.ts as

declare module "*.css" {
   const css: string;
   export default css;
}

But is there way to declare import with assertion?

import styles from "./styles.css" assert { type: "css" }; /* import the styles as a CSSStyleSheet */
@jleeson
Copy link
Owner

jleeson commented Nov 17, 2023

@BoldyrevDA Unfortunately it does not appear that TypeScript currently has a way to declare ambient declarations for import assertions. You can read more on that at microsoft/TypeScript#46135

However depending on your use for this plugin, you can change the import behavior of the string import to be a CSS Module rather than using assert syntax. In order to do this, add the modules: true option to the plugin config, and then you should be able to update your declaration.d.ts with the following:

declare module "*.css" {
   const stylesheet: CSSStyleSheet;
  export default stylesheet;
}

Its worth noting that when using modules: true, your css imports will be non-standard and importing styles as a string will not work anymore.

@jleeson
Copy link
Owner

jleeson commented Dec 14, 2023

Closing this since its a question regarding TypeScript syntax support.

@jleeson jleeson closed this as completed Dec 14, 2023
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

2 participants