fix(types): fix import('echarts/types/dist/shared')
in users' .d.ts can not visit 'echarts/types/dist/shared.d.ts'
since v5.5.0
#20030
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Brief Information
This pull request is in the type of:
What does this PR do?
Fix that In users' .d.ts
import('echarts/types/dist/shared')
can not visit'echarts/types/dist/shared.d.ts'
since v5.5.0.Fixed issues
Fix #19663 .
Relative issues: ecomfe/vue-echarts#766 , #19513
Details
Before: What was the problem?
In users' .d.ts
import('echarts/types/dist/shared')
can not visit'echarts/types/dist/shared.d.ts'
since v5.5.0.It cause that users can only get type
any
when import echarts type.After: How does it behave after the fixing?
After some investigate to
tsc
, I found that:Test case (tested in typescript v5.3.3 and v4.6.4) :
In the case about we can found that:
tsc
may generate "inline import" (i.e.,import(xxx/xxx/xxx)
) or not.tsc
may intentionally generate inline import to avoid to handle name collision. see Somtimes generating inline imports for d.ts files instead of using globally imported symbols microsoft/TypeScript#30258 (comment) TS 3.7 generates inline imports for typing files (.d.ts) although the source contains imports at the top microsoft/TypeScript#36097 (comment).import(xxx/xxx/xxx)
, the pathxxx/xxx/xxx
probably goes deep into the file where the target type declared literally defined, but may not consider its visibility defined in "exports" field ofpackage.json
.EChartsType
from the last sentenceexport let option = {} as EChartsType1;
, andEChartsType
is declared inecharts/types/dist/shared.d.ts
, the final inline import isimport('echarts/types/dist/shared')
orimport('echarts/types/dist/shared.js')
, althoughecharts/types/dist/shared
seems to be a private file and imported byecharts/core.d.ts
.tsc
. It's verbose, just for the record:transformRoot
,function symbolToTypeNode
is called to resolve the literal typeEChartsType1
of the sentenceexport let option = {} as EChartsType1;
,lookupSymbolChain
is called to use the inputsymbol(of "EChartsType")
to get a chain like[symbol(of "dist/shared.d.ts"), symbol(of "EChartsType")]
, and generate a specifier forchain[0]
, and callconst lit = factory.createLiteralTypeNode(factory.createStringLiteral(specifier));
to create a literal node containing the path 'xxx/dist/shared', and callreturn factory.createImportTypeNode(lit, ...)
to return a new type node on which the literal node is mounted.writeKeyword("import"); writePunctuation("("); emit(node.argument);
the literal node of 'xxx/dist/shared' generated above is in thenode.argument
and be used to write asimport('xxx/dist/shared')
.ambient module declaration file
ornormal module file
has the same behavior.import('echarts/types/dist/shared')
orimport('echarts/types/dist/shared.js')
can be generated bytsc
.import('echarts/types/dist/shared.js')
:"moduleResolution": "node"
in "tsconfig.json".tsc
can resolve it back toecharts/types/dist/shared.d.ts
(tested in tsc v4.6.4 & v5.3.3).import('echarts/types/dist/shared')
:"moduleResolution": "bundle"
in "tsconfig.json".echarts/types/dist/shared
is not visible from outside. So this PR make it visible by add it to the "exports" field ofpackage.json
.Other infomation
In https://cdn.jsdelivr.net/npm/[email protected]/dist/index.d.ts there are both
import("echarts/types/dist/shared")
andimport("echarts/types/dist/echarts")
. It because in https://github.com/ecomfe/vue-echarts/blob/v6.7.3/src/types.ts#L2 both from 'echarts/core' and 'echarts'.It's OK, but it probably be better a little if all of them are
from 'echarts/core'
, @Justineo .In the user's vue project, after update the dependent echarts, if the type is still
any
in.vue
file in vscode, try to "> Vue: Restart Vue and TS servers"