question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Export `ECharts` class in TypeScript

See original GitHub issue

Version

5.0

What problem does this feature solve?

The init function returns a class of type ECharts. This works fine as long as you use the chart variable right after initalization.

import {init, EChartsOption} from "echarts";
...
export class BaseChart {
  initChart() {
    const chart = init(nativeElement);
    // do something else with the chart.
  }
}

But all typings get lost, when you want to pass the chart around (e.g. for updating data later on)

import {init, EChartsOption} from "echarts";
...
export class BaseChart {
  const chart: ECharts; // <--- This does not work. You can it make `any`, but then all typings are lost;
  initChart() {
    this.chart = init(nativeElement);
    // do something else with the chart.
  }
}

The TypeScript definitions do not export the ECharts class

import {ECharts} from "echarts" // <---- This is not possible

Edit Using EChartsType which is extending ECharts does not work too.

import {init, EChartsOption} from "echarts";
import {EChartsType} from "echarts/lib/echarts";
...
export class BaseChart {
  initChart() {
    const chart: EChartsType = init(nativeElement);
    // This doesn't work too
    // TS2322: Type 'ECharts' is not assignable to type 'EChartsType'.
    // Types have separate declarations of a private property '_zr'.
  }
}

What does the proposed API look like?

Extend the exports in TypeScript. Something like this:

export { ECharts, EChartsFullOption as EChartsOption, connect, disConnect, dispose, getInstanceByDom, getInstanceById, getMap, init, registerLocale, registerMap, registerTheme };

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
hesamatcommented, Dec 4, 2020

Further to your comment, we are relying on the other types like BarSeriesOption, ChartDataset, etc. This change in v5 will result in losing all of the typings in our code base. Also, as a result of not exporting them none of the fields in the EchartsOptions have any types. Please consider exporting them all

2reactions
emeryaocommented, Dec 21, 2020

@ilueckel Hi, we will plan to export more types

before that, you can use

type EChartsInstance = ReturnType<init>;

as a workaround.

In my case the workaround above does not work but the codes below worked around

import * as echarts from 'echarts';
type ECharts = ReturnType<typeof echarts.init>;
let chart = echarts.init(someElement);

echarts@v5.0.0 TypeScript@v4.0.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

Export `ECharts` class in TypeScript · Issue #13755
The TypeScript definitions do not export the ECharts class. import {ECharts} from "echarts" // <---- This is not possible.
Read more >
Using Apache ECharts with React and TypeScript
If you happen to use Apache ECharts, this feed may help you integrate it with your React + TypeScript codebase.
Read more >
Is it possible to use ECharts Baidu with Angular 2 and ...
They use require as a function but typescript has its own require. ... export class AdvertReportComponent implements OnInit { echarts: any ...
Read more >
ngx-echarts
Start using ngx-echarts in your project by running `npm i ngx-echarts`. ... TypeScript icon, indicating that this package has built-in type ...
Read more >
Using ECharts with TypeScript - 羡辙杂俎 - Wenli Zhang
Now, echarts can be accessed in your TypeScript file. ... export class Page3 { constructor() { console.log(echarts); // works here } } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found