# What's the type of any Task?

**URL:** <https://forums.swift.org/t/whats-the-type-of-any-task/68372>\
**Category:** Using Swift\
**Created:** [November 9, 2023, 9:54pm UTC](https://forums.swift.org/t/whats-the-type-of-any-task/68372 "2023-11-09T21:54:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manfred](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@manfred](https://forums.swift.org/u/manfred)\
**Post date:** [November 9, 2023, 9:54pm UTC](https://forums.swift.org/t/whats-the-type-of-any-task/68372/1 "2023-11-09T21:54:04Z")

</div>

Let's say I want to record an arbitrary Task in a variable so that I can cancel it later. What should the type of this variable be?

**var** task: Task\<Any, Never\>

**let** foo = Task {

**return**"Hello World!"

}

**let** bar = Task {

**return** 42

}

task = foo // Cannot assign value of type 'Task\<String, Never\>' to type 'Task\<Any, Never\>'

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [November 9, 2023, 10:15pm UTC](https://forums.swift.org/t/whats-the-type-of-any-task/68372/2 "2023-11-09T22:15:50Z")

</div>

Existentials to the rescue.

```swift
protocol Cancellable {
    func cancel()
}

extension Task: Cancellable {}

var task: Cancellable!

let foo = Task {
    "Hello World!"
}

let bar = Task {
    42
}

task = foo
task.cancel()

```

---

<div class="post-metadata">

**Author:** ![manfred](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@manfred](https://forums.swift.org/u/manfred)\
**Post date:** [November 12, 2023, 5:18pm UTC](https://forums.swift.org/t/whats-the-type-of-any-task/68372/3 "2023-11-12T17:18:39Z")

</div>

That's a solution to this particular problem. Thanks.
