0

I'm making a library and users should be able to create a custom object that extends a TypeScript interface. This custom object is then passed to a library function that merges the custom object in an object with defaults and returns the result.

The custom object that extends the TypeScript interface can override methods from the interface. I want to set this in the method to the custom object. Currently I have this (failing) code:

interface Thing<T extends SpecialThing = SpecialThing> {
    a: number
    derp(this: T, blah?: Partial<T>): T
}

interface SpecialThing extends Thing {
    b: string
}

function createThing<T extends Thing<T>>(options: T) {
    return Object.assign({
        a: 1,
        derp() {}
    }, options)
}

// userland:

interface CustomThing extends SpecialThing {
    c: boolean
}

createThing<CustomThing>({
    a: 1,
    b:'b',
    c: true,
    derp() {
        this.c // `this` should be CustomThing
        return this
    }
})

See the TypeScript playground. SpecialThing is the interface users can extend, Thing is more used internally.

It would be ok to only have SpecialThing, but I need the type variable T to pass to derp(this: T) and it seems silly to have users extend SpecialThing like so:

interface CustomThing extends SpecialThing<CustomThing> {
    c: boolean
}
4
  • 1
    Uh, hmm... I'd normally suggest using polymorphic this for such behavior, but in your particular case, why do you need anything like that at all? What about this code doesn't meet your needs?
    – jcalz
    Feb 11, 2021 at 21:06
  • Unfortunately I need the type variable for the derp method. I've updated the example to show why (it needs it for another argument and a return value).
    – Flauwekeul
    Feb 12, 2021 at 14:45
  • 1
    Okay, then does polymorphic this work well enough for you, as in this code?
    – jcalz
    Feb 12, 2021 at 14:47
  • Yes! 😃 Once I added this: this to derp's signature it worked. Didn't know about polymorphic this, thanks a lot. Also for the superfast replies 👍 You want to post an answer or shall I answer it myself?
    – Flauwekeul
    Feb 12, 2021 at 14:52

1 Answer 1

1

In cases where an object type needs to make reference to itself that automatically propagates down to subtypes, you can often just write this for the type. This is called polymorphic this and can take the place of the sort of F-bounded polymorphism you see in Java and other languages of the form interface Foo<T extends Foo<T>> {...}. Since your Thing looks like that to me, I'd see what happens if you remove the T generic type parameter and replace all references to T with this:

interface Thing {
    a: number
    derp(blah?: Partial<this>): this
}

interface SpecialThing extends Thing {
    b: string
}

function createThing<T extends Thing>(options: T) {
    return Object.assign({
        a: 1,
        derp() { }
    }, options)
}

interface CustomThing extends SpecialThing {
    c: boolean
}

createThing<CustomThing>({
    a: 1,
    b: 'b',
    c: true,
    derp() {
        this.c
        return this
    }
})

This all works as expected to me. Note that didn't include the this: this parameter in derp() because you might not need it... Generally speaking the type of this inside a method implementation defaults to the polymorphic this type anyway, so there's not an obvious advantage to explicitly spelling it out. It doesn't hurt though, and there might be some situations in which it is important but I'm not sure, and it would depend on the use case.

Anyway, that's my recommendation, given the example code. I see that it's possible that there might be a disconnect between Thing and SpecialThing, (your original code had T extends SpecialThing and not T extends Thing<T>), so you could run into something weird with the b property. You can probably address it if you need to with this & SpecialThing instead of this alone, but again, that depends on the use case.

Playground link to code

1
  • In the actual code I needed to add this: this (else this becomes any in userland). Not sure why, but my example is a lot simpler than the actual code. Also, it should be T extends SpecialThing as in my first example. Thanks a lot for the extensive answer.
    – Flauwekeul
    Feb 12, 2021 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.