Skip to content

Commit

Permalink
Remove inaccurate section about type alias names
Browse files Browse the repository at this point in the history
Likely true at one point, but right now aliases actually do create a name that is used for Intellisense and errors. However, if you make aliases of another alias it just uses the first alias name; this has been marked as a bug, but it's possibly intended behavior: microsoft/TypeScript#19198
  • Loading branch information
jayphelps authored Nov 7, 2017
1 parent 94a843e commit 4b4f879
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions pages/Advanced Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,7 @@ type Yikes = Array<Yikes>; // error

## Interfaces vs. Type Aliases

As we mentioned, type aliases can act sort of like interfaces; however, there are some subtle differences.

One difference is that interfaces create a new name that is used everywhere.
Type aliases don't create a new name &mdash; for instance, error messages won't use the alias name.
In the code below, hovering over `interfaced` in an editor will show that it returns an `Interface`, but will show that `aliased` returns object literal type.

```ts
type Alias = { num: number }
interface Interface {
num: number;
}
declare function aliased(arg: Alias): Alias;
declare function interfaced(arg: Interface): Interface;
```

A second more important difference is that type aliases cannot be extended or implemented from (nor can they extend/implement other types).
The most important difference is that type aliases cannot be extended or implemented from (nor can they extend/implement other types).
Because [an ideal property of software is being open to extension](https://en.wikipedia.org/wiki/Open/closed_principle), you should always use an interface over a type alias if possible.

On the other hand, if you can't express some shape with an interface and you need to use a union or tuple type, type aliases are usually the way to go.
Expand Down

0 comments on commit 4b4f879

Please sign in to comment.