Skip to content

Commit

Permalink
doc: update docs/typescript.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 14, 2023
1 parent 8f97826 commit 3c2e988
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ const stringBox = new Box("a package")

```ts
class Location {
constructor(public x: number, public y: number) {}
constructor(
public x: number,
public y: number
) {}
}
const loc = new Location(20, 40);

Expand All @@ -633,27 +636,33 @@ TypeScript 特定于类的扩展,可自动将实例字段设置为输入参数
abstract class Animal {
abstract getName(): string;
printName() {
console.log("Hello, " + this.getName());
console.log("Hello, " + this.getName());
}
}
class Dog extends Animal { getName(): { ... } }
class Dog extends Animal {
getName(): { ... }
}
```

一个类可以被声明为不可实现,但可以在类型系统中被子类化。 class 成员也可以。

### 装饰器和属性
<!--rehype:wrap-class=col-span-2-->

```ts
import { Syncable, triggersSync, preferCache, required } from "mylib"
import {
Syncable, triggersSync, preferCache,
required
} from "mylib"

@Syncable
class User {
@triggersSync()
save() { ... }
@preferCache(false)
get displayName() { ... }
update(@required info: Partial<User>) { ... }
update(@required info: Partial<User>) {
//...
}
}
```

Expand All @@ -676,6 +685,21 @@ class MyClass {

类可以声明索引签名,与其他对象类型的索引签名相同。

### 在 forwardRef 上面声明泛型

```ts
export const Wrapper = forwardRef(
<T extends object>
(
props: RootNodeProps<T>,
ref: React.LegacyRef<HTMLDivElement>
) => {
return (
<div ref={ref}></div>
);
}
```
实用程序类型
----
Expand Down

0 comments on commit 3c2e988

Please sign in to comment.