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
Copy file name to clipboardExpand all lines: basics/type-of-object-interfaces.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -56,8 +56,8 @@ let xcatliu: Person = {
56
56
website: 'http://xcatliu.com',
57
57
};
58
58
59
-
// index.ts(9,3): error TS2322: Type '{ name: string; age: number; github: string; }' is not assignable to type 'Person'.
60
-
// Object literal may only specify known properties, and 'github' does not exist in type 'Person'.
59
+
// index.ts(9,3): error TS2322: Type '{ name: string; age: number; website: string; }' is not assignable to type 'Person'.
60
+
// Object literal may only specify known properties, and 'website' does not exist in type 'Person'.
61
61
```
62
62
63
63
可见,**赋值的时候,变量的形状必须和接口的形状保持一致**。
@@ -126,7 +126,7 @@ let xcatliu: Person = {
126
126
};
127
127
```
128
128
129
-
使用 `[propName: string]`可以定义任意属性是`string`的值的类型。
129
+
使用 `[propName: string]`定义了任意属性取`string`类型的值。
130
130
131
131
需要注意的是,**一旦定义了任意属性,那么确定属性和可选属性都必须是它的子属性**:
132
132
@@ -144,15 +144,15 @@ let xcatliu: Person = {
144
144
};
145
145
146
146
// index.ts(3,3): error TS2411: Property 'age' of type 'number' is not assignable to string index type 'string'.
147
-
// index.ts(7,5): error TS2322: Type '{ [x: string]: string | number; name: string; age: number; github: string; }' is not assignable to type 'Person'.
147
+
// index.ts(7,5): error TS2322: Type '{ [x: string]: string | number; name: string; age: number; website: string; }' is not assignable to type 'Person'.
148
148
// Index signatures are incompatible.
149
149
// Type 'string | number' is not assignable to type 'string'.
150
150
// Type 'number' is not assignable to type 'string'.
0 commit comments