Skip to content

Commit

Permalink
update(web-TypeScript): implicit index signature error
Browse files Browse the repository at this point in the history
issue #105
  • Loading branch information
sabertazimi committed Aug 4, 2021
1 parent 7572010 commit 51a54a0
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions notes/web/javascript/typescriptBasicNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,13 +808,49 @@ class Something extends React.Component<{ foo: number }, { baz: number }> {

## Index Signature

For `JavaScript`,
implicitly calls `toString` on any object index signature:

```js
let obj = {
toString(){
console.log('toString called')
return 'Hello'
}
}

let foo: any = {};
foo[obj] = 'World'; // toString called
console.log(foo[obj]); // toString called, World
console.log(foo['Hello']); // World
```

TypeScript will give an error to prevent beginners
from doing such things.

**Index Signature Error**: `Element implicitly has an 'any' type
because expression of type 'string' can't be used to index type XXX`
can fixed with

- `Record<string, T>`.
- explicit **const** propertyName type:

```ts
// propertyName should be extends keyof T
function getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] {
return o[propertyName]; // o[propertyName] is of type T[K]
}
```

### Index Signature Type Checking

```ts
let x: { foo: number; [x: string]: any };

x = { foo: 1, baz: 2 }; // ok, 'baz' 属性匹配于索引签名
```

当你声明一个索引签名时,所有明确的成员都必须符合索引签名
当你声明一个索引签名时,所有明确的成员都必须符合索引签名:

```ts
// ok
Expand All @@ -832,7 +868,7 @@ interface Bar {
}
```

使用交叉类型可以解决上述问题
使用交叉类型可以解决上述问题:

```ts
type FieldState = {
Expand Down

0 comments on commit 51a54a0

Please sign in to comment.