Errata

Programming TypeScript

Errata for Programming TypeScript

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Chapter 4
Chapter 4 - Polymorphism

The code fragment after

"Let’s start by pulling out filter’s full type signature, and adding some placeholder unknowns for the types:"

type Filter = {
(array: unknown, f: unknown) => unknown[]
}

does not compile with error TS1005: ':' expected.

The code should be:

type Filter = {
(array: unknown, f: unknown): unknown[]
}

Alexei Ersov  Jun 17, 2020 
Chapter 4. Functions
Overloaded Function Types

The note on overloading a function declaration has this listing with createElement function:

function createElement(tag: 'a'): HTMLAnchorElement
function createElement(tag: 'canvas'): HTMLCanvasElement
function createElement(tag: 'table'): HTMLTableElement
function createElement(tag: string): HTMLElement {
// ...
}

It is not the same as previous createElement. It is missing an overload for string. It is not possible to call the second createElement with a string other than 'a', 'canvas', and 'table'. For example createElement('foo') results in

error TS2769: No overload matches this call.
Overload 1 of 3, '(tag: "a"): HTMLAnchorElement', gave the following error.
Argument of type '"foo"' is not assignable to parameter of type '"a"'.
Overload 2 of 3, '(tag: "canvas"): HTMLCanvasElement', gave the following error.
Argument of type '"foo"' is not assignable to parameter of type '"canvas"'.
Overload 3 of 3, '(tag: "table"): HTMLTableElement', gave the following error.
Argument of type '"foo"' is not assignable to parameter of type '"table"'.

Corrected listing:

function createElement(tag: 'a'): HTMLAnchorElement
function createElement(tag: 'canvas'): HTMLCanvasElement
function createElement(tag: 'table'): HTMLTableElement
function createElement(tag: string): HTMLElement // this line was missing
function createElement(tag: string): HTMLElement {
// ...
}

Alexei Ersov  Jun 20, 2020 
Chapter 4. Functions
Overloaded Function Types

The createElement example

type CreateElement = {
(tag: 'a'): HTMLAnchorElement
(tag: 'canvas'): HTMLCanvasElement
(tag: 'table'): HTMLTableElement
(tag: string): HTMLElement
}

let createElement: CreateElement = (tag: string): HTMLElement => {
// ...
}

results in error

TS2322: Type '(tag: string) => HTMLElement' is not assignable to type 'CreateElement'.
Type 'HTMLElement' is missing the following properties from type 'HTMLAnchorElement': charset, coords, download, hreflang, and 21 more.

Alexei Ersov  Jun 20, 2020 
Printed Page 13
1st line in general note

"... but it's strongly recommend for ..." to
"... but it's strongly recommended for ..."

Anonymous  Sep 02, 2020 
Printed Page 22
footnote 2

"At the time of writing ..." Can you give the concrete version number?

I can understand why you would not want to put the version number on the cover, but it should at least be included in the "Revision History" in the frontmatter, e.g.:

Revision History for the First Edition
2019-04-18: First Release, updated to TypeScript 3.4
2019-08-09: Second Release, updated to TypeScript 3.5

Anonymous  Sep 02, 2020 
PDF Page 34
1st paragraph

" notice that everything but c and h is implicitly typed", implicitly should be explicitly.

Dong  Oct 26, 2023 
Printed Page 51
footnote 2

You Don't Know JS

should presumably be italicized or quoted as it is a title.

Anonymous  Sep 03, 2020 
Printed Page 60
Figure: 4.3 TypeError when missing a combined overload signature

When talking about overloaded functions, following the code example given in the book, the type error message in figure 4.3 is wrong.

Since we've defined the code above that our Reserve function will return a Reservation type, the error message should say:

"Type '(from: any, to: any, destination: any) => void' is not assignable to type 'Reserve'.
Type 'void' is not assignable to type 'Reservation'"

Oana Munteanu  Apr 25, 2021 
Printed Page 61
Bottom code example

When discussing overload function types, I noticed the following example provides a comment that should be switched:

type Reservation = string

type Reserve = {
(from: Date, to: Date, destination: string): Reservation
(from: Date, destination: string): Reservation
}

let reserve: Reserve = (
from: Date,
toOrDestination: Date | string,
destination?: string
) => {
if (toOrDestination instanceof Date && destination !== undefined) {
return "Book a one-way trip"
} else if (typeof toOrDestination === 'string') {
return "Book a round trip"
}

return "You are not going anywhere"
}

console.log(reserve(new Date(), new Date(), 'Bali'))
console.log(reserve(new Date(), 'Bali'))

In this example, we are saying that sending three arguments means the caller wants to book a one way trip. However, when calling the reserve function, when you send three arguments, that means they want to book a round trip.

To resolve this minor mistake, simply switch the following comments in the book:

// Book a one-way trip
// Book a round trip

Should be in this order:

// Book a round trip
// Book a one-way trip



Justin Page  Jun 20, 2020 
Printed Page 61
Last code snippet in the page

The comment in the list if else block seems swapped.
if toOrDestination is Date and destination is not undefined that means its a two way trip. The book says Book a one way trip.

Similarly in the else if it should be one way trip instead of round trip

Amit Jha  Jul 23, 2020 
Printed Page 61
Last paragraph (example)

In the function implementation of `Reserve`, you've implied that if the `toOrDestination` parameter is an instance of Date and undefined, that it refers to the one-way trip overload case, when I believe this refers to a round-trip case. Very minor issue, but can cause confusion when trying to connect this example to the function overload described previously.

Function overload:
type Reserve = {
(from: Date, to: Date, destination: string): Reservation;
(from: Date, destination: string): Reservation;
}

Implementation (comments below should be swapped):
let reserve: Reserve = (
from: Date,
toOrDestination: Date | string,
destination?: string
) => {
if (toOrDestination instanceof Date && destination !== undefined) {
// Book a one-way trip
} else if (typeof toOrDestination === 'string') {
// Book a round trip
}
....
}

Evan Bruchet  Oct 06, 2021 
Printed Page 63
last two lines of the penultimate paragraph

... e.g., createEle[space]
ment('foo') ...

the broken word does not extend to the right margin.

It looks like the line-filling algorithm reached a limit -- maybe changing the word "doesn't" to "does not" will help it fill the whole line.

Anonymous  Sep 03, 2020 
PDF Page 65
First code line

Currently the code of function full signature shows :
type Filter = {
(array: unknown, f: unknown) => unknown[]
}
this gives error as the format of the full call signature is not right. It should be:
type Filter = {
(array: unknown, f: unknown): unknown[]
}
I checked the previous one in VSCode and it shows error. So i guess this is syntax error.
I hope you will look into it.
Regards.

Manoj Kumar Singh  Mar 05, 2024 
Printed Page 79, 80
Section "Generic Type Defaults", 2nd and 4th code snippet

In the following code snippet exemplifying how you can explicitly bind a generic type to MyEvent<T>,

let buttonEvent: MyEvent<HTMLButtonElement> = {
target: myButton,
type: string
}

and in the fourth code snippet featured in the same section:

let myEvent: MyEvent = {
target: myElement,
type: string
}

Given that both of the snippets feature assignment statements with object literal expressions on the right-hand side, shouldn't the "type" property have either a string literal or a variable holding a string value as its value?

Steve Jo  Jul 09, 2020 
Mobi Page 177
Paragraph just before "Mapped Types"

The text says:
---
Record gives you one extra degree of freedom compared to regular object index signatures: with a regular index signature you can constrain the types of an object’s values, but the key can only be a regular string, number, or symbol; with Record, you can also constrain the types of an object’s keys to subtypes of string and number.
---
However, as far as I understand, we can also constraint the types of an object's keys to subtypes of string and number, for example:

type MyTpe = { [k in Weekday]: Day; };

Weekday is:

type Weekday = 'Mon' | 'Tue'| 'Wed' | 'Thu' | 'Fri'

so, it is not just string.

Anonymous  Aug 15, 2023 
Printed Page 218
2nd Paragraph

"However, at the time of writing...." is pretty annoying to read in our fast-moving field. Perhaps it would help some readers to include URLs like https://caniuse.com/?search=javascript+import in such situations. Please ask your publisher to make use of such resources.

Anonymous  Sep 07, 2020 
9999
First code sample under "Typesafe APIs" in Chapter 9

The the argument to `fetch` in the `get` example (search for `await fetch`) is missing backticks around it's string argument. The line is:

let res = await fetch(/api/${entry})

and should be

let res = await fetch(`/api/${entry}`)

PS This form requires a page number, but I see no way to determine a page number in Safari Online.

Ethan Brown  Sep 06, 2020