---
title: Trait
framework: packagedescription
role: symbol
role_heading: Structure
path: 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

### Initializers

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

### Instance Properties

- [description](packagedescription/trait/description.md)
- [enabledTraits](packagedescription/trait/enabledtraits.md)
- [name](packagedescription/trait/name.md)

### Type Methods

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

## Relationships

### Conforms To

- [Equatable](swift/equatable.md)
- [ExpressibleByExtendedGraphemeClusterLiteral](swift/expressiblebyextendedgraphemeclusterliteral.md)
- [ExpressibleByStringLiteral](swift/expressiblebystringliteral.md)
- [ExpressibleByUnicodeScalarLiteral](swift/expressiblebyunicodescalarliteral.md)
- [Hashable](swift/hashable.md)

## See Also

### Configuring Traits

- [traits](packagedescription/package/traits.md)
