TypeScript-Handbook icon indicating copy to clipboard operation
TypeScript-Handbook copied to clipboard

Outdated Interfaces vs. Type Aliases

Open Hkattelu opened this issue 6 years ago • 2 comments

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms: Interfaces, Type Aliases Code

Expected behavior: The docs for Interfaces vs. Type aliases are outdated. They state that types don't create names for errors whereas interfaces do create names. This isn't true in more recent versions of typescript. In the first playground link, I test this expecting to see no type name for the error in the line where I assign to the type "Point". Additionally, I expect that I shouldn't be able to extend or implement type aliases as the docs say that I can't.

Actual behavior: You can see that the errors in each line clearly state the type name of the regardless of whether or not it is a type or interface. You can also see that when I try to extend/implement a type alias, everything transpiles like how you would expect, with no errors

Playground Link: http://www.typescriptlang.org/play/#src=type%20Point%20%3D%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%20%20%20%20y%3A%20number%3B%20%0D%0A%7D%0D%0A%0D%0Ainterface%20PointInterface%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%20%20%20%20y%3A%20number%3B%20%0D%0A%7D%0D%0A%0D%0Aconst%20typeInstance%3A%20Point%20%3D%20%7Bx%3A%201%2C%20z%3A%201%7D%3B%0D%0Aconst%20interfaceInstance%3A%20PointInterface%20%3D%20%7Bx%3A%201%2C%20z%3A1%7D%3B

http://www.typescriptlang.org/play/#src=type%20Point%20%3D%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%20%20%20%20y%3A%20number%3B%20%0D%0A%7D%0D%0A%0D%0Ainterface%20ThreeDimensionPoint%20extends%20Point%20%7B%0D%0A%20%20%20%20z%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Aclass%20ThreeDimensionPointClass%20implements%20Point%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%20%20%20%20y%3A%20number%3B%20%0D%0A%20%20%20%20z%3A%20number%3B%0D%0A%7D

Related Issues: https://github.com/Microsoft/TypeScript/issues/28980

Hkattelu avatar Dec 13 '18 16:12 Hkattelu

cf. https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c

I've also read people say that this article is also outdated. I'm not sure.

This section of the docs hasn't been updated in 3 years. https://github.com/Microsoft/TypeScript-Handbook/blame/master/pages/Advanced%20Types.md#L448

It would be helpful if someone who completely understands the difference could rewrite this section.

deklanw avatar Jan 20 '19 22:01 deklanw

Great one, partially duplicated in https://github.com/Microsoft/TypeScript/issues/28980

pawelgrzybek avatar Apr 09 '19 05:04 pawelgrzybek