Typescript SnippetsIf you look closely, you'll notice that most of your favorite NPM packages aren't written in Javascript. They're written in a fancy, new language, called "Typescript" - and for a good reason. Typescript helps you discover everything your favorite NPM packages have to offer: all of their functions, classes and interfaces. But Typescript has a steep learning curve, and your first Typescript file is the hardest to write. These snippets make it easier.
Usage:The best way to take advantage of Typescript is to organize your code into classes. But classes are intricate: they have static methods, private variables, private methods, instance methods, accessors and constructors. If you're new to Typescript development, it can be hard to remember these pieces when you're staring at a blank document. Lucky for you, the Once you write your typescript code, you probably want to share it with someone else. But, before you do that, you should document what it does, and why you wrote it, with TSdoc. Typescript's first-class support for classes, interfaces, and type declarations makes it easy for programmers to independently write pieces of code that fit together. However, typescript doesn't share the context behind the code. The context includes:
TSdoc is the standard way to add context to your code. It splits your documentation into sections, each of which corresponds to a single function, variable, class or method. It further splits each section into tags. Each tag describes a different aspect of the section. Together, they give other developers a comprehensive understanding of the code you wrote. As an added bonus, TSdoc also formats your documentation into a website or markdown file, with Typedoc. Use TSdoc SectionsTo use TSdoc, you need to write your documentation inside multi-line comments:
If you don't like manually typing out multi-line comments. No problem, just use the TSdoc lumps everything within a multiline comment into a single section:
To split your documentation into sections, all you need to do is make a multiline comment for each section. But where do you put each section? TSdoc expects you to put each section directly before the piece of code it describes:
Every TSdoc section contains a summary, a set of blocks and a list of modifiers:
The summary should contain up to three sentences that explains what the corresponding code does. E.g.:
Each block contains a single tag, followed by supporting documentation. You can even format this documentation with markdown and code fences if you want.
The list of modifiers contains tags separated by spaces, none of which are followed by text. E.g.:
If all of this is a lot to remember, don't worry, I've made a few clips that make it a LOT easier: Use the
|
Tag | Trigger | What it does: |
---|---|---|
@decorator | decorator |
Quotes an ES6 decorator expression. |
@deprecated | deprecated |
Deprecates the code it documents, and recommends an up-to-date alternative. |
@defaultValue | defaultValue |
Lists value that a property of a class or interface will have if it isn't set. |
@example | example |
Demonstrates how to use the code it documents. |
@param | param |
Describes an argument of a method or function. |
@privateRemarks | privateRemarks |
Contains documentation that should be omitted from any auto-generated documentation site. |
@remarks | remarks |
Contains an explanation of the implementation details, reasoning, or any other long-form contextual information about the code it documents. |
@returns | returns |
Describes what returns from a method or function. |
@see | see |
Lists links to other sections of the documentation or websites. |
@throws | throws |
Lists any errors that a method or function throws. |
@typeParam | typeParam |
Describes the types you can insert into the type argument of a generic function, interface or class. |
Tags that describe phrases within blocks (also known as inline tags):
Tag | Trigger | What it does: |
---|---|---|
@inheritDoc | inheritDoc |
Copies the @remarks @params @typeParam from another section into the current section. |
@label | label |
Adds an arbitrary label to the block that contains it, so that the block can be referenced it. |
@link | link |
Links to another section, or a website. |
Tags that describe modifiers
Note that the @mod
clip expands to: @alpha @beta @eventProperty @experimental @internal @override @packageDocumentation @public @readonly @sealed @virtual
. This includes all of the available modifier clips, each of which occupies its own tab stop. This makes it east to choose the ones you want. Keep in mind that you must place these modifiers on the LAST line of the TSdoc comment.
Tag | Trigger | What it does: |
---|---|---|
@alpha | mod |
Marks code as an 'alpha' release. |
@beta | Marks code as a 'beta' release. | |
@eventProperty | Indicates that a method returns a Browser Event or Node Event. | |
@experimental | Marks code as 'experimental' release. | |
@internal | Excludes code from a public API. | |
@override | Marks a class as overriding the class from which it inherits. | |
@packageDocumentation | Indicates that a section describes an entire package - not just the code it immediately precedes. This should only ever be used in the package's entry .d.ts file. |
|
@public | Marks code as a stable, 'public' release. This code shouldn't change. | |
@readonly | Marks a variable or property as being read-only. | |
@sealed | Indicates that a class should never be extended. | |
@virtual | Indicates that a class can not only be extended, but can also be overridden without consequence. |
When you use any of the above clips within a TSdoc section, it will automatically add in the tag name, and a placeholder for any additional text that follows the tag.
How Typescript Snippets works:
This extension contributes the snippets/snippets.code-snippets
file to VScode. When you install this extension in VScode, it grabs the snippets in this file, and offers them as intellisense suggestions when you
Roadmap:
See CHANGELOG.md
Contribute to Typescript Snippets:
Repository Structure:
File or Folder | What does it do? | When should you modify it? |
---|---|---|
.readme/ |
Contains all of the images used in this README. Note that everything in this folder is versioned with Git LFS. | Whenever you need to add or change the images used in this README. |
snippets/snippets.code-snippets |
Contains all of the snippets that this extension provides. | Whenever you need to change any of the snippets that this extension provides. |
.vscodeignore |
Lists the files that will not be included in this extension when its' published. | Never. |
CHANGELOG.md |
Lists the changes that each version introduces. | Whenever you increment this package's version in package.json |
package.json |
Describes the contents, scripts, and configuration details of this extension. | Whenever you need to release a new version of this package. |
README.md |
This file. | Whenever you add or change a snippet in this extension. |
Develop:
See Visual Studio Code ➝ Create your own snippets
Test:
Open this project in VScode, then press
F5
to load the snippets in a new VScode window.Create a new Typescript file, or open an existing one.
Test the snippets you just made.
Whenever you save changes to the snippets, hit the 'reload' button in the debugger tray to send the changes to the VScode window.