You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/Interfaces.md
+8-7
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ printLabel(myObj);
20
20
The type-checker checks the call to `printLabel`.
21
21
The `printLabel` function has a single parameter that requires that the object passed in has a property called `label` of type string.
22
22
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.
24
24
25
25
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:
26
26
@@ -100,10 +100,10 @@ let mySquare = createSquare({color: "black"});
100
100
101
101
# Excess Property Checks
102
102
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; }`.
104
104
We also just learned about optional properties, and how they're useful when describing so-called "option bags".
105
105
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.
107
107
For example, taking our last example using `createSquare`:
Notice the given argument to `createSquare` is spelled *`colour`* instead of `color`.
123
123
In plain JavaScript, this sort of thing fails silently.
124
124
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.
126
126
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.
128
128
Object literals get special treatment and undergo *excess property checking* when assigning them to other variables, or passing them as arguments.
129
129
If an object literal has any properties that the "target type" doesn't have, you'll get an error.
130
130
@@ -149,9 +149,10 @@ let mySquare = createSquare(squareOptions);
149
149
150
150
Since `squareOptions` won't undergo excess property checks, the compiler won't give you an error.
151
151
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.
154
154
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.
0 commit comments