<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/eraseToAnyPublisher()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE010eraseToAnyB0AA0eB0Vy6OutputQz7FailureQzGyF"
  },
  "title" : "eraseToAnyPublisher()"
}
-->

# eraseToAnyPublisher()

Wraps this publisher with a type eraser.

```
func eraseToAnyPublisher() -> AnyPublisher<Self.Output, Self.Failure>
```

## Return Value

An [`AnyPublisher`](/documentation/Combine/AnyPublisher) wrapping this publisher.

## Discussion

Use [`eraseToAnyPublisher()`](/documentation/Combine/Publisher/eraseToAnyPublisher()) to expose an instance of [`AnyPublisher`](/documentation/Combine/AnyPublisher) to the downstream subscriber, rather than this publisher’s actual type.
This form of *type erasure* preserves abstraction across API boundaries, such as different modules.
When you expose your publishers as the [`AnyPublisher`](/documentation/Combine/AnyPublisher) type, you can change the underlying implementation over time without affecting existing clients.

The following example shows two types that each have a `publisher` property. `TypeWithSubject` exposes this property as its actual type, [`PassthroughSubject`](/documentation/Combine/PassthroughSubject), while `TypeWithErasedSubject` uses [`eraseToAnyPublisher()`](/documentation/Combine/Publisher/eraseToAnyPublisher()) to expose it as an [`AnyPublisher`](/documentation/Combine/AnyPublisher). As seen in the output, a caller from another module can access `TypeWithSubject.publisher` as its native type. This means you can’t change your publisher to a different type without breaking the caller. By comparison, `TypeWithErasedSubject.publisher` appears to callers as an [`AnyPublisher`](/documentation/Combine/AnyPublisher), so you can change the underlying publisher type at will.

```
public class TypeWithSubject {
    public let publisher: some Publisher = PassthroughSubject<Int,Never>()
}
public class TypeWithErasedSubject {
    public let publisher: some Publisher = PassthroughSubject<Int,Never>()
        .eraseToAnyPublisher()
}

// In another module:
let nonErased = TypeWithSubject()
if let subject = nonErased.publisher as? PassthroughSubject<Int,Never> {
    print("Successfully cast nonErased.publisher.")
}
let erased = TypeWithErasedSubject()
if let subject = erased.publisher as? PassthroughSubject<Int,Never> {
    print("Successfully cast erased.publisher.")
}

// Prints "Successfully cast nonErased.publisher."
```

---

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)