---
title: Trait
framework: swift-package-manager
role: symbol
role_heading: Structure
path: swift-package-manager/documentation/packagedescription/trait
---

# Trait

A package trait.

## Declaration

```swift
struct Trait
```

## Overview

Overview A trait is a package feature that expresses conditional compilation and potentially optional dependencies. It is typically used to expose additional or extended API for the package. When you define a trait on a package, the package manager uses the name of that trait as a conditional block for the package’s code. Use the conditional block to enable imports or code paths for that trait. For example, a trait with the canonical name MyTrait allows you to use the name as a conditional block: #if MyTrait // additional imports or APIs that MyTrait enables #endif // MyTrait important: Traits must be strictly additive. Enabling a trait must not remove API. If your conditional code requires a dependency that you want to enable only when the trait is enabled, add a conditional declaration to the target dependencies, then include the import statement within the conditional block. The following example illustrates enabling the dependency MyDependency when the trait Trait1 is enabled: targets: [    .target(        name: "MyTarget",        dependencies: [            .product(                name: "MyAPI",                package: "MyDependency",                condition: .when(traits: ["Trait1"])            )        ]    ), ] Coordinate a declaration like the example above with code that imports the dependency in a conditional block: #if Trait1 import MyAPI #endif // Trait1

## Topics

### Declaring Traits

- [trait(name:description:enabledTraits:)](swift-package-manager/documentation/packagedescription/trait/trait(name:description:enabledtraits:).md)
- [default(enabledTraits:)](swift-package-manager/documentation/packagedescription/trait/default(enabledtraits:).md)

### Creating Traits

- [init(name:description:enabledTraits:)](swift-package-manager/documentation/packagedescription/trait/init(name:description:enabledtraits:).md)
- [init(stringLiteral:)](swift-package-manager/documentation/packagedescription/trait/init(stringliteral:).md)

### Inspecting Traits

- [name](swift-package-manager/documentation/packagedescription/trait/name.md)
- [description](swift-package-manager/documentation/packagedescription/trait/description.md)
- [enabledTraits](swift-package-manager/documentation/packagedescription/trait/enabledtraits.md)

### Default Implementations

- [Equatable Implementations](swift-package-manager/documentation/packagedescription/trait/equatable-implementations.md)
- [ExpressibleByExtendedGraphemeClusterLiteral Implementations](swift-package-manager/documentation/packagedescription/trait/expressiblebyextendedgraphemeclusterliteral-implementations.md)
- [ExpressibleByStringLiteral Implementations](swift-package-manager/documentation/packagedescription/trait/expressiblebystringliteral-implementations.md)

## Relationships

### Conforms To

- Swift.Equatable
- Swift.ExpressibleByExtendedGraphemeClusterLiteral
- Swift.ExpressibleByStringLiteral
- Swift.ExpressibleByUnicodeScalarLiteral
- Swift.Hashable

## See Also

### Configuring Traits

- [traits](swift-package-manager/documentation/packagedescription/package/traits.md)
