Fork me on GitHub

Project Notes

#407 VSCode Extensions with Typescript

An initial exploration of writing vscode extensions with Typescript.

Notes

VS Code extensions are just Node.js packages that plug into the editor’s extensibility API. With TypeScript you get type-safe access to that API, compile-time checks and first-class tooling. The fastest way to start is to scaffold a project with the official Yeoman generator (yo code), choose “New Extension (TypeScript)”, and press F5 to launch a second VS Code window that already loads your extension for live debugging .

Key moving parts

  • src/extension.ts – entry file; export an activate() (and optionally deactivate()) function.
  • package.json – lists metadata, activation events and the commands your extension contributes.
  • vscode module – gives you APIs for commands, editors, diagnostics, webviews, Language Server Protocol, etc.
  • Extension Development Host – special VS Code instance spawned by F5 where your code runs; reload it with Ctrl-R to pick up changes.

Typical flow

  1. Run yo code, pick TypeScript, name it, open the folder.
  2. Open src/extension.ts, register a command with vscode.commands.registerCommand.
  3. Add the command id to the commands and activationEvents arrays in package.json.
  4. Press F5 → new window opens → Cmd-Shift-P → run your command → see the info message.
  5. Iterate: edit, reload window, test.
  6. When ready, vsce package (or vsce publish) to share the extension .

That’s it—about 20 lines of TypeScript give you a working command, and from there you can add language support, tree views, webviews, task providers, debug adapters, etc.

The First Tutorial

Following along with https://code.visualstudio.com/api/get-started/your-first-extension

Installing the generator

npm install --global yo generator-code

Running the generator:

$ yo code
# ? What type of extension do you want to create? New Extension (TypeScript)
# ? What's the name of your extension? HelloWorld
### Press <Enter> to choose default for all options below ###

# ? What's the identifier of your extension? helloworld
# ? What's the description of your extension? LEAVE BLANK
# ? Initialize a git repository? Y
# ? Which bundler to use? unbundled
# ? Which package manager to use? npm

# ? Do you want to open the new folder with Visual Studio Code? Open with `code`

The key generated components:

Running the extension:

running

What Next

  • Testing
    • a core part of the extension framework
  • Publishing checklist:
    • vsce CLI installed (npm i -g vsce).
    • package.json has name, version, publisher, engines.vscode, repository and a README.md.
    • Create a publisher ID once at marketplace.visualstudio.com → “Publish extensions”.
    • Generate a Personal Access Token (Pubs > Manage > Tokens) with Marketplace scope.
    • vsce login (paste token) → vsce publish (or vsce package to get a .vsix to side-load).
    • Done: extension is live in the Marketplace within a few minutes.

Credits and References

About LCK#407
toolseditorstypescript

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader