Skip to content

Rename ALBUM_API to MUSIC_API and fix typo #55

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions book-content/chapters/13-modules-scripts-declaration-files.md
Original file line number Diff line number Diff line change
@@ -280,13 +280,13 @@ type Album = {
releaseDate: string;
};

declare const ALBUM_API: {
declare const MUSIC_API: {
getAlbumInfo(upc: string): Promise<Album>;
searchAlbums(query: string): Promise<Album[]>;
};
```

Because we haven't included any imports or exports, this file is treated as a script. This means that the `ALBUM_API` variable is now available globally in our project.
Because we haven't included any imports or exports, this file is treated as a script. This means that the `MUSIC_API` variable is now available globally in our project.

#### Scoping Global Variables To One File

@@ -301,17 +301,17 @@ type Album = {
releaseDate: string;
};

declare const ALBUM_API: {
declare const MUSIC_API: {
getAlbumInfo(upc: string): Promise<Album>;
searchAlbums(query: string): Promise<Album[]>;
};

export function getAlbumTitle(upc: string) {
return ALBUM_API.getAlbumInfo(upc).then((album) => album.title);
return MUSIC_API.getAlbumInfo(upc).then((album) => album.title);
}
```

Now, `ALBUM_API` is only available in the `musicUtils.ts` file. `declare` defines the value within the scope it's currently in. So, because we're now inside a module (due to the `export` statement), `ALBUM_API` is scoped to this module.
Now, `MUSIC_API` is only available in the `musicUtils.ts` file. `declare` defines the value within the scope it's currently in. So, because we're now inside a module (due to the `export` statement), `MUSIC_API` is scoped to this module.

#### `declare const`, `declare var`, `declare let`, `declare function`

@@ -343,7 +343,7 @@ type Album = {
// ---cut---
// inside musicUtils.ts
declare global {
declare const ALBUM_API: {
declare const MUSIC_API: {
getAlbumInfo(upc: string): Promise<Album>;
searchAlbums(query: string): Promise<Album[]>;
};
@@ -356,14 +356,14 @@ This almost works, except for the error. We can't use `declare` inside an ambien
// inside musicUtils.ts

declare global {
const ALBUM_API: {
const MUSIC_API: {
getAlbumInfo(upc: string): Promise<Album>;
searchAlbums(query: string): Promise<Album[]>;
};
}
```

Now the `ALBUM_API` variable has been put into the global scope.
Now the `MUSIC_API` variable has been put into the global scope.

### `declare module`

2 changes: 1 addition & 1 deletion book-content/chapters/16-the-utils-folder.md
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ const getFirstElement = <TMember>(arr: TMember[]) => {

Just like generic functions, it's common to prefix your type parameters with `T` to differentiate them from normal types.

Now when we call `getFirstElement`, TypeScript will infer the type of `` based on the argument we pass in:
Now when we call `getFirstElement`, TypeScript will infer the type of `TMember` based on the argument we pass in:

```ts twoslash
const getFirstElement = <TMember>(arr: TMember[]) => {