Formatter
TypeSpec comes with a built-in formatter. The formatter can be used in different ways:
- Via the cli
- Via the VS Code/vs extension
- As a
prettier
plugin
Via the cli
Section titled āVia the cliāFormat all TypeSpec files:
tsp format "**/*.tsp"
Check file formatting without modifying them, useful for CI enforcement.
tsp format --check "**/*.tsp"
Via the VS Code or VS extension
Section titled āVia the VS Code or VS extensionāWhen you use the extensions for VS Code or Visual Studio, the tsp formatter becomes automatically accessible.
If youāre working within a TypeSpec file, you can format the document using the default keyboard shortcut for formatting, alt+shift+F
.
Configuration - Prettier
Section titled āConfiguration - PrettierāIf a prettier config (.prettierrc.yaml
, .prettierrc.json
, etc.) is present in the project, the formatter will use the configuration from there.
By default this will then use the typespec style guide without any explicit option.
Configuration - VS Code
Section titled āConfiguration - VS CodeāFor VS Code to respect the TypeSpec standard style set the following options style
{ ["typespec"]: { "editor.detectIndentation": false, "editor.insertSpaces": true, "editor.tabSize": 2, }}
Configuration - EditorConfig
Section titled āConfiguration - EditorConfigāIf using .editorconfig
with the editor config extension
[*.tsp]indent_size = 2indent_style = space
Via prettier
Section titled āVia prettierāThe tsp formatter is essentially a prettier
plugin. If you already have a prettier
configuration set up for other languages, it can be quite handy to simply integrate TypeSpec into this existing pipeline.
In your prettier
config file, add:
plugins: - "./node_modules/@typespec/prettier-plugin-typespec"overrides: [{ "files": "*.tsp", "options": { "parser": "typespec" } }]