From 5674f8a65801c970dd4446ea943cd938d9117391 Mon Sep 17 00:00:00 2001 From: iugo Date: Fri, 2 Feb 2024 15:52:02 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=96=AD=E8=A8=80=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ts/object.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ts/object.ts b/ts/object.ts index 80d3c4e..b61eb88 100644 --- a/ts/object.ts +++ b/ts/object.ts @@ -14,3 +14,10 @@ export function isUnknownObject(v: unknown): v is UnknownObject { } return true; } + +/** 断言为未知对象 */ +export function assertUnknownObject(v: unknown): asserts v is UnknownObject { + if (typeof v !== 'object' || v === null) { + throw new TypeError('Expected an object'); + } +} From 756d4a1a43c461338281aee70ce6ad0da2531582 Mon Sep 17 00:00:00 2001 From: iugo Date: Fri, 2 Feb 2024 17:12:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=96=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ts/object.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ts/object.ts b/ts/object.ts index b61eb88..787b3b7 100644 --- a/ts/object.ts +++ b/ts/object.ts @@ -21,3 +21,13 @@ export function assertUnknownObject(v: unknown): asserts v is UnknownObject { throw new TypeError('Expected an object'); } } + +/** + * 断言为数组 + * @param v 需要断言的变量 + */ +export function assertArray(v: unknown): asserts v is Array { + if (!Array.isArray(v)) { + throw new TypeError('Expected an array'); + } +} From 6800a6a566e7c012ba87e1a137a7199c0b1454be Mon Sep 17 00:00:00 2001 From: iugo Date: Fri, 2 Feb 2024 17:12:21 +0800 Subject: [PATCH 3/4] =?UTF-8?q?chore:=20=E5=AE=8C=E5=96=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 38163df..98b2dc8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,5 +13,8 @@ "editor.formatOnSave": true, "[sql]": { "editor.formatOnSave": false + }, + "[json]": { + "editor.defaultFormatter": "denoland.vscode-deno" } } From 04f2812d6dba5a504b4a5896a352f1562849ba53 Mon Sep 17 00:00:00 2001 From: iugo Date: Fri, 2 Feb 2024 17:16:23 +0800 Subject: [PATCH 4/4] =?UTF-8?q?doc:=20=E5=AE=8C=E5=96=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ts/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ts/README.md diff --git a/ts/README.md b/ts/README.md new file mode 100644 index 0000000..6977d65 --- /dev/null +++ b/ts/README.md @@ -0,0 +1,10 @@ +# TypeScript 相关函数说明 + +该文件夹内的代码分为三种: + +1. 类型. +2. 类型判断函数. +3. 类型断言函数. + +类型断言函数在已知 API 结构等场景下非常有用. 类型断言函数不同于普通的 `as` 断言, +`as` 断言一般不会做任何检查, 而类型断言函数而是真的做了一些检查.