Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Last active January 10, 2021 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractmachines/cf30f91be9bd828d5604b2d1a50cbacc to your computer and use it in GitHub Desktop.
Save abstractmachines/cf30f91be9bd828d5604b2d1a50cbacc to your computer and use it in GitHub Desktop.
Custom TS types for Webpack + React ES6-Style Imports

Custom Typescript Types Recipes for React + Webpack ES6 style imports

use case: CSS files in /src/styles dir, and SVG and PNG files in /assets/images dir

  • custom.d.ts file in dir of svg, png assets:
declare module "*.svg" {
    const content: any;
    export default content;
}

declare module "*.png" {
    const content: any;
    export default content;
}
  • custom.d.ts file in styles dir alongside CSS files:
declare module "*.css" {
    const content: any;
    export default content;
}
  • include those files/custom types, in tsconfig.json in project root:
{
  "compilerOptions": {
    "outDir": "dist",
    "sourceMap": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react"
  },
  "include": ["./src/**/*", "src/styles/custom.d.ts", "assets/images/custom.d.ts"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment