I'm trying to assign a property to a class instance where the property's signature is a function that's passed `this` as a type.

```
function create<R>(returnValue: R) {
  function wrap(d?: string): R | string {
    if (d) return d
    return returnValue
  }
  return wrap
}

class A {
  prop = create(this)
}

```

It works so long as the function returned from `create` isn't overloaded.

```
function create<R>(returnValue: R) {
  // if no argument, return returnValue
  // otherwise return the argument
  function wrap(d: string): string
  function wrap(): R
  function wrap(d?: string): R | string {
    if (d) return d
    return returnValue
  }
  return wrap
}

```

As soon as I overload the returned function to refine the return type based on passed in arguments, TypeScript complains with:

```
The inferred type of 'prop' references an inaccessible 'this' type. A type annotation is necessary.

```

I'd be happy to add a type annotation, but every signature I've tried has exactly the same problem.

Hardcoding the `this` value to the class `A` won't work because I also want to be able to support subclassing.

I'm not sure how to work around this. Happy to add ugly hacks in the class so long as the resulting types are correct.

```js
function create<R>(returnValue: R) {
  // if no argument, return returnValue
  // otherwise return the argument
  function wrap(d: string): string
  function wrap(): R
  function wrap(d?: string): R | string {
    if (d) return d
    return returnValue
  }
  return wrap
}

class A {
  // Currently gives the error:
  // The inferred type of 'prop' references an inaccessible 'this' type. A type annotation is necessary.
  // If we remove the overloading from the wrap function above, this part works,
  // but it breaks the chaining interface because it doesn't know if the result of new A().prop()
  prop = create(this)
}

class B extends A {
  isB = true // something to differentiate (prevents hardcoding A as return type)
}

const a: A = new A().prop()
const b: B = new B().prop()
b.prop('something')

```

[Playground Link](https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABBATgUwIZTQHgEoB8AFOlCCmAGoYA2IaAXIngJSIDeAsAFCKID0-RDGCIwcRBhQBzEAFs0YKABpEpcknUVqdNDz6DEcKAAs0KAO4wAzmjVoyFRKbtTZCpfsShIsBIgsUDAAHIgATJmsoFBgwaRZI6NjpLx9oeCRAkKIE5lTwdP8s0LCAfkSYuNy8RAAfRCjK6Q4vPhFEcLYtJDDW+0dNBw0dei8AXy9ugKDgngnuHggaDGtrRABBFt4BIQBhcnQlGgBPRGkYADc0NZdEcxQ4FAYvQwAVM2EwYHu0MOdj4J2OCiADkwQewRB9m+hwg10kSFiGAgcNWMAARjQ7CDTDYoVAAWgAHQbf6AhHiKBYDLCNZgNCo6xSY5El5CACSogsdnQcjgV2cH355hocAwYWS3geckFdmK3gKfiQGHRwtUuLWwSkUACjwA1tZlGzEOiQDqYDr0egMAbZcgTBhYpLYtgUMBkXZ0QyMCBbMIdWE4NcwDjEHrxBZhKJbuhrCAaDrgWI0JH1jkieC4KEWF5M8FEABeZDW7BEDU57jzRbLVaIABCdwAHtgwGE1psuNsbA2i9F6DsGnAFLi4s4JBLgDDFLAsHYiOC0FclGsHSgwhA4BLR5sVv0NGS0BWq9wN2AopImJsi-TU+m8zlFghz+imD3k5G63eIQ-uOiM9+QWsIcHBMZIQQrHggA)

Would also appreciate any explanation as to why this doesn't work.