# Mysterious failure of extensions declared on typealiases of protocols. Any good workaround?

**URL:** <https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653>\
**Category:** Using Swift\
**Tags:** extensions, protocols, typealias\
**Created:** [October 4, 2022, 7:22pm UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653 "2022-10-04T19:22:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielhall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danielhall/32/29455_2.png) [@danielhall](https://forums.swift.org/u/danielhall)\
**Post date:** [October 4, 2022, 7:22pm UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/1 "2022-10-04T19:22:10Z")

</div>

If I declare a typealias for a concrete type in a protocol, e.g.

```swift
struct A { }

protocol P {
  typealias Element = A
}

```

and then I write an extension based on that typealias on a conforming type, e.g.:

```swift
struct Conforming: P { }

extension Conforming.Element { // This should be identical to `extension A`
  var success: Bool { true }
}

```

The compiler allows it. But trying to access the extension property will fail:

```swift
print(Conforming.Element().success) //Compiler error: "Value of type 'Conforming.Element' (aka 'A') has no member 'success'"

```

I declare the extension directly on the type (not through the typealias), e.g.

```swift
extension A {
  var success: Bool { true }
}

```

Then the same print statement (accessing type `A` through the protocol typealias) will succeed

Declaring the typealias directly on the type allows the extension through the typealias to work:

```swift
struct Conforming {
  typealias Element = A
}

extension Conforming.Element { 
  var success: Bool { true }
}

print(Conforming.Element().success) // "true"

```

It only seems to fail on the requirement of writing an extension that refers to a typealias declared by a protocol.

Is this a bug? Does anyone have any ideas for workarounds?

Ultimately what I want to accomplish is something like this (just a convenience really)

```swift
protocol SettingsProvider { 
  typealias SettingsConfiguration = Configuration<Self>
}

struct Configuration<T> { }

struct ConcreteType: SettingsProvider { }

// I want to be able to do this
extension ConcreteType.Settings {
  var someProperty: String
}

// Instead of this equivalent that seems much less intuitive to me
extension Configuration<ConcreteType> {
  var someProperty: String
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 4, 2022, 7:46pm UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/2 "2022-10-04T19:46:47Z")

</div>

> [@danielhall](#):
>
> Is this a bug?

Yup, seems like a bug. The workaround would be to declare the extension without using the alias, as you're doing.

---

<div class="post-metadata">

**Author:** ![danielhall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danielhall/32/29455_2.png) [@danielhall](https://forums.swift.org/u/danielhall)\
**Post date:** [October 5, 2022, 12:09am UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/3 "2022-10-05T00:09:50Z")

</div>

Thanks Xiaodi!

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 5, 2022, 1:10am UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/4 "2022-10-05T01:10:01Z")

</div>

Would you mind filing a bug over on the Swift GitHub issues tracker?

---

<div class="post-metadata">

**Author:** ![danielhall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danielhall/32/29455_2.png) [@danielhall](https://forums.swift.org/u/danielhall)\
**Post date:** [October 5, 2022, 4:27pm UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/5 "2022-10-05T16:27:37Z")

</div>

Done!

> <https://github.com/apple/swift/issues/61450>
>
> \*\*Describe the bug\*\*
> 
> If I declare a typealias for a concrete type in a protoc…ol, e.g.
> 
> \`\`\`
> struct A { }
> 
> protocol P {
> typealias Element = A
> }
> \`\`\`
> 
> and then I write an extension based on that typealias on a conforming type, e.g.:
> 
> \`\`\`
> struct Conforming: P { }
> 
> extension Conforming.Element { // This should be identical to \`extension A\`
> var success: Bool { true }
> }
> 
> \`\`\`
> 
> The compiler allows it. But trying to access the extension property will fail:
> 
> \`\`\`
> print(Conforming.Element().success) //Compiler error: "Value of type 'Conforming.Element' (aka 'A') has no member 'success'"
> 
> \`\`\`
> If I declare the extension directly on the type (not through the typealias), e.g.
> 
> \`\`\`
> extension A {
> var success: Bool { true }
> }
> \`\`\`
> 
> Then the same print statement (accessing type A through the protocol typealias) will succeed
> 
> Declaring the typealias directly on the type allows the extension through the typealias to work:
> 
> \`\`\`
> struct Conforming {
> typealias Element = A
> }
> 
> extension Conforming.Element { 
> var success: Bool { true }
> }
> 
> print(Conforming.Element().success) // "true"
> \`\`\`
> 
> It only seems to fail on the requirement of writing an extension that refers to a typealias declared by a protocol.
> 
> 
> \*\*Environment (please fill out the following information)\*\*
> - OS: macOS 12.6
> - Xcode Version/Tag/Branch: Version 14.0 (14A309)
> 
> 
> \*\*Additional context\*\*
> 
> Ultimately what I want to accomplish is something like this (just a convenience really)
> 
> \`\`\`
> protocol SettingsProvider { 
> typealias SettingsConfiguration = Configuration\<Self\>
> }
> 
> struct Configuration\<T\> { }
> 
> struct ConcreteType: SettingsProvider { }
> 
> // I want to be able to do this
> extension ConcreteType.Settings {
> var someProperty: String
> }
> 
> // Instead of this equivalent that seems much less intuitive to me
> extension Configuration\<ConcreteType\> {
> var someProperty: String
> }
> \`\`\`
> 
> \[Swift Forums thread here\](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653)

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [October 24, 2022, 5:45pm UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/6 "2022-10-24T17:45:57Z")

</div>

> [@xwu](#):
>
> Yup, seems like a bug. The workaround would be to declare the extension without using the alias, as you're doing.

The bug was that it was not diagnosed. However, the type checker is rejecting the extension because it doesn't fit the implementation model. Extension binding has to happen very early, before other name lookups take place, so it cannot depend on conformance lookup tables having been constructed. For this reason, you cannot extend a protocol type alias accessed as a member of a concrete type.

I already fixed the diagnostic on main a while ago so I'm closing the bug: [Sema: Improved check for failure to bind an extension by slavapestov · Pull Request #60364 · apple/swift · GitHub](https://github.com/apple/swift/pull/60364)

---

<div class="post-metadata">

**Author:** ![danielhall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danielhall/32/29455_2.png) [@danielhall](https://forums.swift.org/u/danielhall)\
**Post date:** [November 8, 2022, 4:50am UTC](https://forums.swift.org/t/mysterious-failure-of-extensions-declared-on-typealiases-of-protocols-any-good-workaround/60653/7 "2022-11-08T04:50:45Z")

</div>

Thanks so much for your rapid attention and helpful explanation @Slava_Pestov!
