Last active
March 19, 2020 10:18
-
-
Save zhaomengru2015/0e0c019eb5e7758cd6199267074842a2 to your computer and use it in GitHub Desktop.
type vs interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type PointType = { | |
| x: number | |
| y: number | |
| } | |
| interface ThreeDimesions extends PointType { | |
| z: number | |
| } | |
| interface PointInterface { | |
| x: number | |
| y: number | |
| } | |
| class RectangleType implements PointType { | |
| x = 2 | |
| y = 4 | |
| } | |
| interface ThreeDimesionsInterface extends PointInterface { | |
| z: number | |
| } | |
| class RectangleInterface implements PointInterface { | |
| x = 3 | |
| y = 4 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Box { | |
| height: number | |
| width: number | |
| } | |
| interface Box { | |
| scale: number | |
| } | |
| const box: Box ={height: 5, width: 6, scale: 10} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface PointInterface { | |
| x: number | |
| y: number | |
| } | |
| type PointType = { | |
| x: number | |
| y: number | |
| } | |
| const getRectangleAreaInterface = (args: PointInterface) => | |
| args.x * args.y | |
| const getRectangleAreaAliased = (args: PointType) => | |
| args.x * args.y | |
| getRectangleAreaInterface({ x: 12 }) | |
| getRectangleAreaAliased({x:2}) |
Author
Author
“A second more important difference is that type aliases cannot be extended or implemented from”(implements-extends.ts)
Author
“type aliases cannot extend/implement other types” (implements-extends-types.ts)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
“One difference is, that interfaces create a new name that is used everywhere. Type aliases don’t create a new name — for instance, error messages won’t use the alias name.” (naming.ts)