> ## Documentation Index
> Fetch the complete documentation index at: https://docs.typesync.org/llms.txt
> Use this file to discover all available pages before exploring further.

# generate-swift

Generates Swift type definitions for the specified schema and writes them to the specified file. Typesync first "flattens" your schema, creating new aliases for inline types defined within. It then generates the primary and auxiliary structs, enums, aliases, encoders, and decoders.

For document models, generated Swift structs include an `@DocumentID var id: String?` property by default. The Firebase Apple SDK fills this value from the document path when decoding and excludes it from the encoded document body when writing.

## Usage

```bash theme={null}
typesync generate-swift --definition <filePathOrPattern> --target <targetEnvironment> --outFile <filePath> --indentation <indentation> --debug <debug>
```

## Options

<ParamField type="string" path="definition" required>
  The exact path or a Glob pattern to the definition file or files. Each definition file must be a YAML or JSON file containing model definitions.

  * Example single file path: `definition/models.json`
  * Example Glob pattern: `'definition/**/*.yml'`
</ParamField>

<ParamField type="string" path="target" required>
  The target environment for which the types are generated. This option specifies the target SDK and version, ensuring
  that the output is compatible with the chosen environment. See the list of available targets [here](#targets).
</ParamField>

<ParamField type="string" path="outFile" required>
  The path to the output file.
</ParamField>

<ParamField type="int" path="indentation" default={2}>
  Indentation or tab width for the generated code.
</ParamField>

<ParamField type="boolean" path="debug" default={false}>
  Whether to enable debug logs.
</ParamField>

## Targets

* `firebase@10`: For Swift projects that rely on the [Firebase Apple SDK (v10)](https://github.com/firebase/firebase-ios-sdk).

## Document IDs

Document model structs import `FirebaseFirestore` and include an `@DocumentID`
property:

```swift theme={null}
import FirebaseFirestore

struct User: Codable {
  @DocumentID var id: String?
  var username: String
}
```

If a document body already has a Firestore field named `id`, rename the generated
document ID property on that document model:

```yaml theme={null}
User:
  model: document
  path: users/{userId}
  swift:
    documentIdProperty:
      name: documentId
  type:
    type: object
    fields:
      id:
        type: string
      username:
        type: string
```

This produces `@DocumentID var documentId: String?` while keeping the body field
named `id`.

## Swift field names

Use `swift.name` on a field when the generated Swift property name should differ
from the Firestore field name. Typesync keeps encoding and decoding aligned with
the original Firestore key through `CodingKeys`.

```yaml theme={null}
User:
  model: document
  path: users/{userId}
  type:
    type: object
    fields:
      class:
        type: string
        swift:
          name: userClass
```

Typesync fails generation if two fields resolve to the same Swift property name,
or if a body field conflicts with the generated `@DocumentID` property. Rename
one of the properties with `swift.name` or `swift.documentIdProperty.name`.
