Style guide
This guide offers a recommended set of naming conventions to follow when drafting a TypeSpec specification.
The guidelines in this article are used in TypeSpec Core libraries. You can use them, or adapt them to your needs. The primary objectives are consistency and readability within your project, team, organization, or company source code.
Naming convention
Type | Naming | Example |
---|---|---|
scalar | camelCase | scalar uuid extends string; |
model | PascalCase | model Pet {} |
model property | camelCase | model Pet {furColor: string} |
enum | PascalCase | enum Direction {} |
enum member | camelCase | enum Direction {up, down} |
namespace | PascalCase | namespace Org.PetStore |
interface | PascalCase | interface Stores {} |
operation | camelCase | op listPets(): Pet[]; |
operation params | camelCase | op getPet(petId: string): Pet; |
unions | PascalCase | union Pet {cat: Cat, dog: Dog} |
unions variants | camelCase | union Pet {cat: Cat, dog: Dog} |
alias | camelCase or PascalCase depending on context | alias myString = string or alias MyPet = Pet |
decorators | camelCase | @format , @resourceCollection |
functions | camelCase | addedAfter |
file name | kebab-case | my-lib.tsp |
template parameter | PascalCase | <ExampleParameter> |
Layout convention
TypeSpec has a built-in formatter. See formatter section for more information on how to use it.
- Use 2 space indenting
- Place a space before an opening curly brace
- Block opening curly brace
{
should be on the same line
- Add a newline after blocks
- Place no space between an operation/decorator/function name and the parameter list
- Do not add spaces inside parentheses
- Add spaces inside curly braces.
- Do not add space inside square brackets
- Start all comments with a space
- Avoid trailing spaces at the end of lines.
Model layout
- Properties should hug each other unless they have decorators or comments
- Wrap properties in new lines if they have leading comments or decorators