zrender
zrender copied to clipboard
Group 的 _children 可以暴露出来吗,或者有别的情况拿到Group的 所有child
属性“_children”为私有属性,只能在类“Group”中访问。ts(2341)
使用了 group.eachChild 会报错,类型“Element<ElementProps>”上不存在属性“shape”。
不应该使用 _ 开头的私有属性,可以用 .childrenRef()
拿到引用,.children()
拿到副本。
不应该使用 _ 开头的私有属性,可以用
.childrenRef()
拿到引用,.children()
拿到副本。
试了下,可以拿到,但是forEach的时候,依然会报错,跟 ecahChild一样,类型“Element<ElementProps>”上不存在属性“shape”。ts(2339)
带 shape 的应该是 Element 子类型, 遍历时可以在回调参数签名中指定下具体子类型。例如:
this.group.eachChild(function (child: LargeSymbolPath) {
// ...
})
带 shape 的应该是 Element 子类型, 遍历时可以在回调参数签名中指定下具体子类型。
this.group.eachChild(function (child: LargeSymbolPath) { // ... })
好的,我试试,多谢
heartGroup.eachChild((heart: zrender.Heart, index) => {
我这么写的,
参数“heart”和“el” 的类型不兼容。 类型“Element<ElementProps>”缺少类型“Heart”的以下属性: shape, getDefaultShape, buildPath, path 及其他 54 项。
能否再贴出一些上下文?heartGroup 的类型是?
const heart = new zrender.Heart()
...
const heartGroup = new zrender.Group();
...
heartGroup.add(heart);
heartGroup.eachChild((heart: zrender.Heart, index) => {}) // 这里的类型报错
参数“heart”和“el” 的类型不兼容。
类型“Element”缺少类型“Heart”的以下属性: shape, getDefaultShape, buildPath, path 及其他 54 项。
我这里试了一下,没有报错,你的 ts 版本是多少?
我这里试了一下,没有报错,你的 ts 版本是多少?
"typescript": "^4.1.2",
我这里试了一下,没有报错,你的 ts 版本是多少?
老哥,这里你那边是ok的吗
我没发现报错。ECharts 中就是这么使用的,可以参考下。https://github.com/apache/echarts/search?q=eachChild
@wayward-man 请问你是编辑器提示错误,还是编译的时候报错? 我这里试了一下,指定具体子类型后,编译没问题,但是vscode中提示跟你一样的错误,这时候在tsconfig里面添加到include,就没有这个错误提示了. 不知道跟你是不是一样的问题.
@wayward-man 请问你是编辑器提示错误,还是编译的时候报错? 我这里试了一下,指定具体子类型后,编译没问题,但是vscode中提示跟你一样的错误,这时候在tsconfig里面添加到include,就没有这个错误提示了. 不知道跟你是不是一样的问题.
嗯,是编译时的报错,你是怎么做的?
@wayward-man 请问你是编辑器提示错误,还是编译的时候报错? 我这里试了一下,指定具体子类型后,编译没问题,但是vscode中提示跟你一样的错误,这时候在tsconfig里面添加到include,就没有这个错误提示了. 不知道跟你是不是一样的问题.
嗯,是编译时的报错,你是怎么做的?
@wayward-man 我这里如果像 (heart: zrender.Heart) => {} 这样指定了类型,编译的时候没有错。
编辑器提示错误,就把文件添加到tsconfig的include里面,就没错误信息了。 我的tsconfig像下面这样
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "esnext",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"types": [
"jest",
"node"
],
"baseUrl": "./src",
"moduleResolution": "Node"
},
"include": [
"src/**/*"
],
"typedocOptions": {
"ignoreCompilerErrors": true,
"mode": "modules",
"out": "./docs",
"excludeNotExported": false,
"stripInternal": false,
}
}
代码在src里面,添加到include了。这样编辑器用的linter也就没有报错。