Skip to content Skip to sidebar Skip to footer

Typescript D3 V4 Import Not Working

I am trying to build a tiny JS library on top of D3 to draw a line chart. I am fairly new to the whole scene, but I thought I'd learn best by jumping in the 'deep end'. Here is the

Solution 1:

Your npm installs related to d3 should be as follows:

If you intend to use only a subset of the modules, for each module you need to install the module itself and the corresponding definition file.

E.g.: npm install d3-array --save and npm install @types/d3-array --save The actual d3-array module will be a proper dependency (not a devDependency as it appears in your snippet above). The definitions from @types may be --save or --save-dev that depends on your use case (for a library used by other code, a proper dependency should be used)

When you want to use the D3 modules with a module loader, you can then import standard TypeScript syntax:

import * as  d3Array from 'd3-array';
import {select, Selection} from 'd3-selection';

Or similar, depending on your needs.

For convenience you could create a simple "bundling" module so that you can access your custom bundle in a more consolidated form:

// d3-bundle.ts
export * from 'd3-array';
export * from 'd3-axis';
...
export * from 'd3-time-format';

You can tailor this module to your needs, including re-exporting only a subset of the members of the individual modules using export {...} from 'd3-MODULE';

In whatever module you want to use D3 now, you would import 'd3-bundle' using an appropriate relative path and you will have access to what you put through the bundle barrel:

// use-d3.ts
import { csv, DsvRequest } from './d3-bundle'; // again, use the right relative path for your project

class LineChart {
  data(url: string): DsvRequest {
      // code to go here
  }
}

Post a Comment for "Typescript D3 V4 Import Not Working"