'why doesn't TreeDataProvider exist in vscode?

I followed this link https://code.visualstudio.com/docs/extensions/yocode to create a vscode plugin project in javascript. I want to use TreeDataProvider to create a tree view but this class is not found from vscode module.

const { TreeItemCollapsibleState, TreeDataProvider } = require('vscode');

The above code return undefined to TreeDataProvider. Below is my package.json dependencies:

"devDependencies": {
    "@types/mocha": "^2.2.42",
    "@types/node": "^7.0.43",
    "eslint": "^4.11.0",
    "typescript": "^2.6.1",
    "vscode": "^1.1.17"
  }

Does anyone know how to require this class in a javascript project?



Solution 1:[1]

Unfortunately a very late answer, but I just stumbled upon this today.

TreeDataProvider is an interface, not a class, which means:

  • your class implements (TypeScript syntax!) and not extends it,
  • it only exists in TS and, like all other type information, is not preserved after being transpiled to JavaScript.

To implement this interface in JS, you just have to implement its methods. Here's what they look like in TS:

// T is the type of your element (of course irrelevant in JS)
getTreeItem(element: T): vscode.TreeItem | Thenable<vscode.TreeItem>
getChildren(element: T): vscode.ProviderResult<T[]>
// optionally:
onDidChangeTreeData?: vscode.Event<void | T| T[] | null | undefined> | undefined;

The TreeDataProvider interface is just there to make it easier to write an implementation that is full and proper.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tored