DEV Community

Discussion on: TypeScript: type vs interface

Collapse
 
stereobooster profile image
stereobooster

One more thing: as I said interface allows extension even after declaration, so it would be possible extend interface exposed by 3-rd party library, but I can't imagine good use case for it. Any ideas?

Collapse
 
tevescastro profile image
Vítor Castro

There’s an example of the usefulness of this in a older than the current version of a functional programming library fp-ts.
It’s called declaration merging and it was used in that library, that implements higher kinded types, to extend those types.

Collapse
 
stereobooster profile image
stereobooster

Can you please point me to the code (link to the file/commit in github)? I want to understand it better

Thread Thread
 
tevescastro profile image
Vítor Castro • Edited

Yes, you can have a look here:
github.com/gcanti/fp-ts/tree/1.x

Has I explained this particular feature was used extensively in V1, but I think that is no longer the case due to the way the library is now structured.

Collapse
 
bolt04 profile image
David Pereira

An example where you'd want to extend the interface of a 3rd party API is OrderCloud. They have the property xp to enable you to extend their data model. Here's an example of a type where xp is object (I think sometimes it's any too).
And the extension would be something like:

interface OrderExtended extends Order {
    xp: OrderXp | null
}
export interface OrderXp {
  isCool: boolean
  isAwesome: boolean
  maybeIsBoth: boolean
}