<!--
{
  "availability" : [
    "SwiftPM: 6.1.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "PackageDescription",
  "identifier" : "/documentation/PackageDescription/Trait",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "PackageDescription"
    ],
    "preciseIdentifier" : "s:18PackageDescription5TraitV"
  },
  "title" : "Trait"
}
-->

# Trait

A package trait.

```
struct Trait
```

## 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:

```swift
#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:

```swift
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:

```swift
#if Trait1
import MyAPI
#endif // Trait1
```

## Topics

### Initializers

[`init(name: String, description: String?, enabledTraits: Set<String>)`](/documentation/PackageDescription/Trait/init(name:description:enabledTraits:))

Creates a trait with a name, a description, and set of additional traits it enables.

[`init(stringLiteral: StringLiteralType)`](/documentation/PackageDescription/Trait/init(stringLiteral:))

Creates a trait with the name you provide.

### Instance Properties

[`var description: String?`](/documentation/PackageDescription/Trait/description)

The trait’s description.

[`var enabledTraits: Set<String>`](/documentation/PackageDescription/Trait/enabledTraits)

A set of other traits of this package that this trait enables.

[`var name: String`](/documentation/PackageDescription/Trait/name)

The trait’s canonical name.

### Type Methods

[`static func `default`(enabledTraits: Set<String>) -> Trait`](/documentation/PackageDescription/Trait/default(enabledTraits:))

Declares the default traits for this package.

[`static func trait(name: String, description: String?, enabledTraits: Set<String>) -> Trait`](/documentation/PackageDescription/Trait/trait(name:description:enabledTraits:))

Creates a trait with a name, a description, and set of additional traits it enables.

## Relationships

### Conforms To

[`ExpressibleByStringLiteral`](/documentation/Swift/ExpressibleByStringLiteral)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

[`ExpressibleByExtendedGraphemeClusterLiteral`](/documentation/Swift/ExpressibleByExtendedGraphemeClusterLiteral)

[`ExpressibleByUnicodeScalarLiteral`](/documentation/Swift/ExpressibleByUnicodeScalarLiteral)

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)