#still a bit confused by the difference between `interface` and `type`

19 messages ยท Page 1 of 1 (latest)

warm hound
#

are there specific cases where you should use one of the other?

hazy raven
#

Afaik, interface is extend-able, but type is not. And interface is sort of made for "object", but for type you can do string and number more. Apart from these subtle difference, i think the two can still be used interchangabily ๐Ÿค”

warm hound
#

feels to me like they could've just made just one keyword to do it all? idk

hazy raven
warm hound
#

thanks

#

but yeah hmm

#

still feel one keyword wouldve been easier haha

oblique fog
# warm hound thanks

Yes, you are right, minus a few edge cases, type is just a more powerful interface. My guess is if they could start from scratch, it'd be just one keyword.

warm hound
#

because of inheritance

oblique fog
#

It's just a weird form of intersection

warm hound
#

yeah i didn't actually know about the & operator until today haha

oblique fog
#

The only thing interfaces can do fundamentally that types can't is use a this type. If support were added for types I wouldn't use interfaces for anything

warm hound
#

do you think they would ever add that in?

oblique fog
#

I couldn't find any issues on GitHub proposing it, though there must be one somewhere. I'd guess probably not.

ArkType allows it ๐Ÿ˜Š

warm hound
#

what's arktype?

oblique fog
hazy raven
# oblique fog The only thing interfaces can do fundamentally that types can't is use a `this` ...

This is not pure typescript. The keyword this can not be used to correlate properties within an object type.

https://stackoverflow.com/a/72521655

oblique fog