Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 9e84c24

Browse files
Addressed review feedback.
1 parent 4d47627 commit 9e84c24

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

pages/Interfaces.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ printLabel(myObj);
2020
The type-checker checks the call to `printLabel`.
2121
The `printLabel` function has a single parameter that requires that the object passed in has a property called `label` of type string.
2222
Notice that our object actually has more properties than this, but the compiler only checks that *at least* the ones required are present and match the types required.
23-
There are some cases where TypeScript isn't as lenient as we'll go over in a bit.
23+
There are some cases where TypeScript isn't as lenient, which we'll cover in a bit.
2424

2525
We can write the same example again, this time using an interface to describe the requirement of having the `label` property that is a string:
2626

@@ -100,10 +100,10 @@ let mySquare = createSquare({color: "black"});
100100

101101
# Excess Property Checks
102102

103-
In our first example using interfaces, TypeScript was okay with giving a value with the type `{ size: number; label: string; }` to something that only expected a `{ label: string; }`.
103+
In our first example using interfaces, TypeScript let us pass `{ size: number; label: string; }` to something that only expected a `{ label: string; }`.
104104
We also just learned about optional properties, and how they're useful when describing so-called "option bags".
105105

106-
However, combining the two naively would let you to shoot youreslf in the foot the same way you might in JavaScript.
106+
However, combining the two naively would let you to shoot yourself in the foot the same way you might in JavaScript.
107107
For example, taking our last example using `createSquare`:
108108

109109
```ts
@@ -122,9 +122,9 @@ let mySquare = createSquare({ colour: "red", width: 100 });
122122
Notice the given argument to `createSquare` is spelled *`colour`* instead of `color`.
123123
In plain JavaScript, this sort of thing fails silently.
124124

125-
One could argue that this program is correctly typed, since the `width` properties are compatible, there's no `color` property present, and the extra `colour` property is insignificant.
125+
You could argue that this program is correctly typed, since the `width` properties are compatible, there's no `color` property present, and the extra `colour` property is insignificant.
126126

127-
However, TypeScript is more opinionated here, and takes the stance that there's probably a bug in this code.
127+
However, TypeScript takes the stance that there's probably a bug in this code.
128128
Object literals get special treatment and undergo *excess property checking* when assigning them to other variables, or passing them as arguments.
129129
If an object literal has any properties that the "target type" doesn't have, you'll get an error.
130130

@@ -149,9 +149,10 @@ let mySquare = createSquare(squareOptions);
149149

150150
Since `squareOptions` won't undergo excess property checks, the compiler won't give you an error.
151151

152-
Keep in mind, for simple code like above, it's questionable whether you should really be trying to "get around" these checks.
153-
For more complex objects literals that have methods and hold state, you may need to keep these techniques in mind, but a majority of excess property errors in user code are actually bugs.
152+
Keep in mind that for simple code like above, you probably should be trying to "get around" these checks.
153+
For more complex object literals that have methods and hold state, you might need to keep these techniques in mind, but a majority of excess property errors in user code are actually bugs.
154154
That means if you're running into excess property checking problems for something like option bags, you might need to revise some of your type declarations.
155+
In this instance, if it's okay to pass an object with both a `color` or `colour` property to `createSquare`, you should fix up the definition of `SquareConfig` to reflect that.
155156

156157
# Function Types
157158

0 commit comments

Comments
 (0)