Skip to main content
Version: Next 🚧

TypeSpec language style guide

This guide offers a recommended set of naming conventions to follow when drafting a TypeSpec specification.

info

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

TypeNamingExample
scalarcamelCasescalar uuid extends string;
modelPascalCasemodel Pet {}
model propertycamelCasemodel Pet {furColor: string}
enumPascalCaseenum Direction {}
enum membercamelCaseenum Direction {up, down}
namespacePascalCasenamespace Org.PetStore
interfacePascalCaseinterface Stores {}
operationcamelCaseop listPets(): Pet[];
operation paramscamelCaseop getPet(petId: string): Pet;
unionsPascalCaseunion Pet {cat: Cat, dog: Dog}
unions variantscamelCaseunion Pet {cat: Cat, dog: Dog}
aliascamelCase or PascalCase depending on contextalias myString = string or alias MyPet = Pet
decoratorscamelCase@format, @resourceCollection
functionscamelCaseaddedAfter
file namekebab-casemy-lib.tsp
template parameterPascalCase<ExampleParameter>
note

In some languages, particularly object-oriented programming languages, it's conventional to prefix certain names with a letter to indicate what kind of thing they are. For example, prefixing interface names with I (as in IPet) or prefixing template parameter names with T (as in TResponse). This is not conventional in TypeSpec.

Layout convention

TypeSpec has a built-in formatter. See formatter section for more information on how to use it.

  • Use 2 space indenting
// bad
model Pet {
name: string;
}

// good
model Pet {
name: string;
}
  • Place a space before an opening curly brace
// bad
model Pet{
name: string;
}

// good
model Pet {
name: string;
}
  • Block opening curly brace { should be on the same line
// bad
model Pet
{
name: string;
}

// good
model Pet {
name: string;
}
  • Add a newline after blocks
// bad
model Pet {
name: string;
}
model Cat extends Pet {}

// good
model Pet {
name: string;
}

model Cat extends Pet {}
  • Place no space between an operation/decorator/function name and the parameter list
// bad
op list (filter: string): Pet[];

// bad
@doc ("This is a pet")

// good
op list(filter: string): Pet[];

// good
@doc("This is a pet")
  • Do not add spaces inside parentheses
// bad
op list( filter: string ): Pet[];

// good
op list(filter: string): Pet[];

  • Add spaces inside curly braces.
// bad
alias foo = {type: "cat"};

// good
alias foo = { type: "cat" };
  • Do not add space inside square brackets
// bad
alias foo = [ 1, 2, 3 ];

// good
alias foo = [1, 2, 3];
  • Start all comments with a space
//bad

// good
  • Avoid trailing spaces at the end of lines.

Model layout

  • Properties should hug each other unless they have decorators or comments
// bad
model Foo {
one: string;

two: string;

three: string;
}

// good
model Foo {
one: string;
two: string;
three: string;
}
  • Wrap properties in new lines if they have leading comments or decorators
// bad
model Foo {
one: string;
@doc("Foo")
two: string;
// line comment
three: string;
/**
* Block comment
*/
four: string;
five: string;
}

// good
model Foo {
one: string;

@doc("Foo")
two: string;

// line comment
three: string;

/**
* Block comment
*/
four: string;

five: string;
}