Skip to content

Conversation

@Obi-Dann
Copy link
Contributor

@Obi-Dann Obi-Dann commented Nov 7, 2019

Fixes #8
At runtime, the module with the style mapping is a commonjs module which exports the values using module.exports. In order to get the typings working for exporting the interface, the locals and the mapping keys, we were using export default as a hack. Which turns out to be problematic in some configurations (see #8) and leads to either failing type check or runtime errors. Instead of using export default, we can leverage TypeScript declaration merging - use const and namespace and with export =.
Example:

// generated d.ts
declare namespace ExampleCssModule {
  export interface IExampleCss {
    "bar-baz": string;
    composed: string;
    foo: string;
  }
}

declare const ExampleCssModule: ExampleCssModule.IExampleCss & {
  /** WARNING: Only available when `css-loader` is used without `style-loader` or `mini-css-extract-plugin` */
  locals: ExampleCssModule.IExampleCss;
};

export = ExampleCssModule;

This type declaration allows the following usage:

import * as styles from "./example.css";
import { IExampleCss } from "./example.css";

console.log(styles.composed);

// or default import when TS `esModuleInterop` enabled
import styles from "./example.css";
import { IExampleCss } from "./example.css";

console.log(styles.composed);

@Obi-Dann Obi-Dann force-pushed the 8-fix-typings-incorrectly-exported-as-defaults branch from 168c73f to 0968e13 Compare November 17, 2019 09:12
@Obi-Dann Obi-Dann force-pushed the 8-fix-typings-incorrectly-exported-as-defaults branch from 0968e13 to 33ed211 Compare November 17, 2019 09:15
@Obi-Dann Obi-Dann changed the title WIP Fix typings incorrectly exported as defaults Fix typings incorrectly exported as defaults Nov 17, 2019
@Obi-Dann Obi-Dann force-pushed the 8-fix-typings-incorrectly-exported-as-defaults branch from 1e25ff8 to c95737c Compare November 17, 2019 09:26
@Obi-Dann Obi-Dann merged commit 9c01f05 into master Nov 17, 2019
@Obi-Dann Obi-Dann deleted the 8-fix-typings-incorrectly-exported-as-defaults branch November 17, 2019 09:35
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

Successfully merging this pull request may close these issues.

TS2339: Property does not exist on type 'typeof import ...'

2 participants