Type::Library(3) User Contributed Perl Documentation Type::Library(3)

# [NAME](#NAME)

Type::Library - tiny, yet Moo(se)-compatible type libraries

# [SYNOPSIS](#SYNOPSIS)

```
   package Types::Mine {
      use Scalar::Util qw(looks_like_number);
      use Type::Library -base;
      use Type::Tiny;
      
      my $NUM = "Type::Tiny"->new(
         name       => "Number",
         constraint => sub { looks_like_number($_) },
         message    => sub { "$_ ain't a number" },
      );
      
      __PACKAGE__->meta->add_type($NUM);
      
      __PACKAGE__->meta->make_immutable;
   }
      
   package Ermintrude {
      use Moo;
      use Types::Mine qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }
   
   package Bullwinkle {
      use Moose;
      use Types::Mine qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }
   
   package Maisy {
      use Mouse;
      use Types::Mine qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }
```

# [STATUS](#STATUS)

This module is covered by the Type-Tiny stability policy.

# [DESCRIPTION](#DESCRIPTION)

Type::Library is a tiny class for creating MooseX::Types-like type libraries which are compatible with Moo, Moose and Mouse.

If you're reading this because you want to create a type library, then you're probably better off reading Type::Tiny::Manual::Libraries.

## [Type library methods](#Type_library_methods)

A type library is a singleton class. Use the "meta" method to get a blessed object which other methods can get called on. For example:

```
   Types::Mine->meta->add_type($foo);
```

[add\_type($type) or add\_type(%opts)](#add_type%28$type%29)

Add a type to the library. If %opts is given, then this method calls "Type::Tiny-&gt;new(%opts)" first, and adds the resultant type.

Adding a type named "Foo" to the library will automatically define four functions in the library's namespace:

"Foo"

Returns the Type::Tiny object.

[is\_Foo($value)](#is_Foo%28$value%29)

Returns true iff $value passes the type constraint.

[assert\_Foo($value)](#assert_Foo%28$value%29)

Returns $value iff $value passes the type constraint. Dies otherwise.

[to\_Foo($value)](#to_Foo%28$value%29)

Coerces the value to the type.

[get\_type($name)](#get_type%28$name%29)

Gets the "Type::Tiny" object corresponding to the name.

[has\_type($name)](#has_type%28$name%29)

Boolean; returns true if the type exists in the library.

"type\_names"

List all types defined by the library.

[add\_coercion($c) or add\_coercion(%opts)](#add_coercion%28$c%29)

Add a standalone coercion to the library. If %opts is given, then this method calls "Type::Coercion-&gt;new(%opts)" first, and adds the resultant coercion.

Adding a coercion named "FooFromBar" to the library will automatically define a function in the library's namespace:

"FooFromBar"

Returns the Type::Coercion object.

[get\_coercion($name)](#get_coercion%28$name%29)

Gets the "Type::Coercion" object corresponding to the name.

[has\_coercion($name)](#has_coercion%28$name%29)

Boolean; returns true if the coercion exists in the library.

"coercion\_names"

List all standalone coercions defined by the library.

[import(@args)](#import%28@args%29)

Type::Library-based libraries are exporters.

"make\_immutable"

Prevents new type constraints and coercions from being added to the library, and also calls "$type-&gt;coercion-&gt;freeze" on every type constraint in the library.

(Prior to Type::Library v2, "make\_immutable" would call "$type-&gt;coercion-&gt;freeze" on every constraint in the library, but not prevent new type constraints and coercions from being added to the library.)

## [Type library exported functions](#Type_library_exported_functions)

Type libraries are exporters. For the purposes of the following examples, assume that the "Types::Mine" library defines types "Number" and "String".

```
   # Exports nothing.
   # 
   use Types::Mine;
   
   # Exports a function "String" which is a constant returning
   # the String type constraint.
   #
   use Types::Mine qw( String );
   
   # Exports both String and Number as above.
   #
   use Types::Mine qw( String Number );
   
   # Same.
   #
   use Types::Mine qw( :types );
   
   # Exports "coerce_String" and "coerce_Number", as well as any other
   # coercions
   #
   use Types::Mine qw( :coercions );
   
   # Exports a sub "is_String" so that "is_String($foo)" is equivalent
   # to "String->check($foo)".
   #
   use Types::Mine qw( is_String );
   
   # Exports "is_String" and "is_Number".
   #
   use Types::Mine qw( :is );
   
   # Exports a sub "assert_String" so that "assert_String($foo)" is
   # equivalent to "String->assert_return($foo)".
   #
   use Types::Mine qw( assert_String );
   
   # Exports "assert_String" and "assert_Number".
   #
   use Types::Mine qw( :assert );
   
   # Exports a sub "to_String" so that "to_String($foo)" is equivalent
   # to "String->coerce($foo)".
   #
   use Types::Mine qw( to_String );
   
   # Exports "to_String" and "to_Number".
   #
   use Types::Mine qw( :to );
   
   # Exports "String", "is_String", "assert_String" and "coerce_String".
   #
   use Types::Mine qw( +String );
   
   # Exports everything.
   #
   use Types::Mine qw( :all );
```

Type libraries automatically inherit from Exporter::Tiny; see the documentation of that module for tips and tricks importing from libraries.

## [Type::Library's methods](#Type::Library)

The above sections describe the characteristics of libraries built with Type::Library. The following methods are available on Type::Library itself.

"setup\_type\_library( $package, $utils, \\@extends )"

Sets up a package to be a type library. $utils is a boolean indicating whether to import Type::Utils into the package. @extends is a list of existing type libraries the package should extend.

# [BUGS](#BUGS)

Please report any bugs to &lt;https://github.com/tobyink/p5-type-tiny/issues&gt;.

# [SEE ALSO](#SEE_ALSO)

Type::Tiny::Manual.

Type::Tiny, Type::Utils, Types::Standard, Type::Coercion.

Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.

# [AUTHOR](#AUTHOR)

Toby Inkster &lt;tobyink@cpan.org&gt;.

# [COPYRIGHT AND LICENCE](#COPYRIGHT_AND_LICENCE)

This software is copyright (c) 2013-2014, 2017-2025 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

# [DISCLAIMER OF WARRANTIES](#DISCLAIMER_OF_WARRANTIES)

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

2026-10-26 perl v5.44.0

---

Document: Type::Library(3pm) from perl-type-tiny 2.010001-r5 (Chainguard OS)
Source: https://github.com/tobyink/p5-type-tiny
