antvis/G6




The issue has been closed
[Suggestion]: imply possible undefined return value for some Graph.get*() methods in document #6784
Crystal-RainSlide posted onGitHub
Describe the bug / é®é¢ęčæ°
Methods like: public getNodeData(id: ID): NodeData;
should be: public getNodeData(id: ID): NodeData | undefined;
.
TypeScript can't infer possible undefined
in array element accesses (array[i]
) like:
An example fix with only getNodeData()
. Can be applied to other get*()
functions.
class Graph {
public getNodeData(): NodeData[];
- public getNodeData(id: ID): NodeData;
+ public getNodeData(id: ID): NodeData | undefined;
public getNodeData(ids: ID[]): NodeData[];
- public getNodeData(id?: ID | ID[]): NodeData | NodeData[] {
+ public getNodeData(id?: ID | ID[]): NodeData | NodeData[] | undefined {
if (id === undefined) return this.context.model.getNodeData();
if (Array.isArray(id)) return this.context.model.getNodeData(id);
- return this.context.model.getNodeData([id])?.[0];
+ return this.context.model.getNodeData([id])?.[0] as NodeData | undefined;
}
}
To check array element accesses for undefined
type safety, either change them to Array.prototype.at()
temporarily, or toggle noUncheckedIndexedAccess
(doc, changelog, issue for making it usable).
Reproduction link / å¤ē°é¾ę„
No response
Steps to Reproduce the Bug or Issue / éē°ę„éŖ¤
const nodeData = new Graph().getNodeData("non-exist-id");
console.assert(typeof nodeData !== undefined);
Version / ēę¬
š 5.x
OS / ęä½ē³»ē»
- macOS
- Windows
- Linux
- Others / å ¶ä»
Browser / ęµč§åØ
- Chrome
- Edge
- Firefox
- Safari (Limited support / ęéęÆę)
- IE (Nonsupport / äøęÆę)
- Others / å ¶ä»