Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a935c56

Browse files
authored
Merge pull request xcatliu#4 from baooab/master
fix `github` to `website` & `任意属性` 小节的表述
2 parents 67584fc + ad87cfe commit a935c56

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

basics/type-of-object-interfaces.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ let xcatliu: Person = {
5656
website: 'http://xcatliu.com',
5757
};
5858

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'.
6161
```
6262

6363
可见,**赋值的时候,变量的形状必须和接口的形状保持一致**
@@ -126,7 +126,7 @@ let xcatliu: Person = {
126126
};
127127
```
128128

129-
使用 `[propName: string]` 可以定义任意属性是 `string` 的值的类型
129+
使用 `[propName: string]` 定义了任意属性取 `string` 类型的值
130130

131131
需要注意的是,**一旦定义了任意属性,那么确定属性和可选属性都必须是它的子属性**
132132

@@ -144,15 +144,15 @@ let xcatliu: Person = {
144144
};
145145

146146
// 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'.
148148
// Index signatures are incompatible.
149149
// Type 'string | number' is not assignable to type 'string'.
150150
// Type 'number' is not assignable to type 'string'.
151151
```
152152

153153
上例中,任意属性的值允许是 `string`,但是可选属性 `age` 的值却是 `number``number` 不是 `string` 的子属性,所以报错了。
154154

155-
另外,在报错信息中可以看出,此时 `{ name: 'Xcat Liu', age: 25, website: 'http://xcatliu.com' }` 的类型被推断成了 `{ [x: string]: string | number; name: string; age: number; github: string; }`,这是联合类型和接口的结合。
155+
另外,在报错信息中可以看出,此时 `{ name: 'Xcat Liu', age: 25, website: 'http://xcatliu.com' }` 的类型被推断成了 `{ [x: string]: string | number; name: string; age: number; website: string; }`,这是联合类型和接口的结合。
156156

157157
## 只读属性
158158

0 commit comments

Comments
 (0)