What problem does this feature solve?
Many eChart's own types are not exported. For example DecalObject
.
A lot of the useful types are not exported by eCharts. So one is either faced with a choice to have untyped objects in one's code or to find a way to get to the un-exported types using TypeScript's powerful type system. For example:
import * as echarts from 'echarts';
type EChartInitParams = Parameters<typeof echarts.init>;
// eCharts docs say `It's only optional when opts.ssr is enabled for server-side rendering.` about the `dom` parameter.
// However, the declared type of this parameter is not optional, so we fix it in our derived type.
type EChartInit = (
dom?: EChartInitParams[0],
theme?: EChartInitParams[1],
opts?: EChartInitParams[2]
) => ReturnType<typeof echarts.init>;
// This type is not exported, so we do a few tricks here to get it.
type EChartInitOptions = Exclude<EChartInitParams[2], undefined>;
export type ChartInitOptions = EChartInitOptions & {
height?: Exclude<EChartInitOptions['height'], string>;
width?: Exclude<EChartInitOptions['width'], string>;
};
or more generally:
type ExportedOrNotWeCanGetToItAnyway_SoMightAsWellMakeItEasierForUs = echarts.ExportedType['propertyWithUnexportedType'];
Given that we mostly can gain access to un-exported types one way or another through exported ones, the whole point of not exporting types is a bit moot.
zrender types used by eCharts are not re-exported by eCharts
For example, we have to add a transient zrender
dependency to our package.json
and do something like the following to access to the type that is part of eChart's public interface but is not re-exported:
import * as zrender from 'zrender';
// then somewhere later in our code
highlight?: {
style?: zrender.PathStyleProps;
...
}
What does the proposed API look like?
For eChart's own types:
import * as echarts from 'echarts';
// echarts.ChartInitOptions is exported, so deriving from it becomes a lot easier now
export type ChartInitOptions = echarts.ChartInitOptions & {
height?: Exclude<echarts.ChartInitOptions['height'], string>;
width?: Exclude<echarts.ChartInitOptions['width'], string>;
};
For zrender types used by eCharts:
import * as echarts from 'echarts';
// No need to add zrender transient dependency to package.json and import types from there because zrender types used by echarts are re-exported by echarts.
highlight?: {
style?: echarts.PathStyleProps;
...
}
2条答案
按热度按时间xqk2d5yq1#
There are a few other issues with types:
legendselectchanged
event params passed into the event's callback function:But when using this type like so:
I would then get a TS error anyway:
Where the
...args: unknown[]
part is coming from here:emeijp432#
yes yes yes!!!!!!!!