Type aliases

Type aliases allow you to create reusable and descriptive data types and resource types.

By using type aliases, you can:
  • Give a type a descriptive name, such as IPv6Addr, instead of creating or using a complex pattern-based type.

  • Shorten and move complex type expressions.

  • Improve code quality by reusing existing types instead of inventing new types.

  • Test type definitions separately from manifests.

Type aliases are transparent, which means they are fully equivalent to the types of which they are aliases. For example, in the following code, the notice returns true because MyType is an alias of the Integer type:
type MyModule::MyType = Integer
notice MyModule::MyType == Integer
Note: The internal types TypeReference and TypeAlias are never values in Puppet code .

Creating type aliases

Use the following syntax to create a type alias:
type <MODULE NAME>::<ALIAS NAME> = <TYPE DEFINITION>
The <MODULE NAME> must be named after the module that contains the type alias, and both the <MODULE NAME> and <ALIAS NAME> begin with a capital letter and must not be a reserved word.
For example, you can create a type alias named MyType that is equivalent to the Integer data type:
type MyModule::MyType = Integer
You can then declare a parameter using the alias as though it were a unique data type:
MyModule::MyType $example = 10

To make your code easier to maintain and troubleshoot, store type aliases as .pp files in your module's types directory, which is a top-level directory and sibling of the manifests and lib directories. Define only one alias per file, and name the file after the type alias name converted to lowercase. For example, MyType is expected to be loaded from a file named mytype.pp.

You can create recursive type aliases, which can refer to the alias being declared or to other types, thereby defining complex, descriptive type definitions without using the Any type. For example:
type MyModule::Tree = Array[Variant[Data, Tree]]
This Tree type alias is defined as a being built out of Arrays that contain Data, or a Tree:
[1,2 [3], [4, [5, 6], [[[[1,2,3]]]]]]
You can also create aliases to resource types:
type MyModule::MyFile = File
When defining an alias to a resource type, use its short form (for example, File) instead of its long form (Resource[File]).