Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start #36

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions book-content/chapters/04-essential-types-and-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ isReleased = "yes";
And also giving us autocomplete on the variable:

```ts
albumTitle.toUpper; // shows `toUpperCase` in autocomplete
albumTitle.toUpper // shows `toUpperCase` in autocomplete
// get rid of the semicolor above - since the semicolon would terminate the auto-complete list, which I think is your point here
```

This is an extremely powerful part of TypeScript. It means that you can mostly _not_ annotate variables and still have your IDE know what type things are.

#### Function Parameters Always Need Annotations
#### Function Parameters Always Need Annotations (unless the function expression itself is typed, as I'm sure you cover later)

But type inference can't work everywhere. Let's see what happens if we remove the type annotations from the `logAlbumInfo` function's parameters:

Expand Down Expand Up @@ -205,7 +206,7 @@ function add(a, b) {

`a` and `b` could be strings, booleans, or anything else. TypeScript can't know from the function body what type they're supposed to be.

So, when you're declaring a named function, their parameters always need annotations in TypeScript.
So, when you're declaring a function declaration, their parameters always need annotations in TypeScript. (function expressions have inferred names, per the ecma 2015 spec - also, your original example in this section was a function expression)

### The `any` Type

Expand Down Expand Up @@ -456,7 +457,7 @@ So TypeScript can not only infer variables, but also the return types of functio

#### Solution 2: Annotating Empty Parameters

As we know, function parameters always need annotations in TypeScript.
As we know, function parameters always need annotations in TypeScript (again only for function declarations or non-typed function expressions).

So, let's update the function declaration parameters so that `a` and `b` are both specified as `string`:

Expand Down