From ef5a27f5a1bf416546240e92664cb5e3ea752f82 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 12 Nov 2022 14:25:47 +0800 Subject: [PATCH] doc: update `typescript.md`. --- docs/typescript.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/docs/typescript.md b/docs/typescript.md index e209e6b..887ce25 100644 --- a/docs/typescript.md +++ b/docs/typescript.md @@ -1264,3 +1264,46 @@ class Select extends React.Component, any> {} // 使用 const Form = () => items={['a', 'b']} />; ``` + +各种各样的技巧 +--- + +### keyof 取 interface 的键 + + +```ts +interface Point { + x: number; + y: number; +} + +// type keys = "x" | "y" +type keys = keyof Point; +``` + +### 索引签名 + +```ts +interface NumberOrString { + [index: string]: string | number; + length: number; + name: string; +} +``` + +### 从数组中提取类型 + +```ts +type Point = { x: number; y: number; } +type Data = Point[]; +// Data 是个数组,提取里面的元素类型 +type PointDetail = Data[number]; +// type PointDetail = { x: number; y: number; } +``` + +### 只读元组类型 + +```ts +const point = [3, 4] as const +// type 'readonly [3, 4]' +```