diff --git a/docs/api/unicloud/function.md b/docs/api/unicloud/function.md index 686c0ff1ca9859d8edeaf0eaf4f13786817eefde..c99ad53483e3e60a2ffe9851eb67db168c38c7eb 100644 --- a/docs/api/unicloud/function.md +++ b/docs/api/unicloud/function.md @@ -11,3 +11,46 @@ + +### 调用云函数时传入泛型 + +> 4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。 + +用法:`uniCloud.callFunction<泛型类型>({name: 'xxx', data: {}} as UniCloudCallFunctionOptions)` + +在不传泛型时callFunction返回的类型为`Promise>`,传入泛型后callFunction返回的类型为`Promise>` + +**代码示例** + +```ts +// 云函数fn代码 +'use strict'; +exports.main = async (event, context) => { + return { + errCode: 0, + errMsg: '', + detail: 'call function detail' + }; +}; +``` + +```ts +// 客户端代码 +type CallFunctionResult = { + errCode : number, + errMsg : string, + detail : string +} +uniCloud.callFunction( + { + name: 'fn', + data: { a: 1 } as UTSJSONObject, + } as UniCloudCallFunctionOptions +).then(function (res) { + const result = res.result // result的类型为CallFunctionResult + const detail = result.detail // 可直接使用.detail访问 + console.log(detail) +}).catch(function (err : any | null) { + console.error(err) +}) +``` diff --git a/docs/api/unicloud/object.md b/docs/api/unicloud/object.md index 91ba78f5ef49f952e4d51b26015000ef636de314..25532a461bc2a2436d50039fc27019024e3ae048 100644 --- a/docs/api/unicloud/object.md +++ b/docs/api/unicloud/object.md @@ -32,4 +32,44 @@ export { // 上面的写法可以自己调整,仅需保证export内包含所 } ``` - \ No newline at end of file + + +### 调用云对象时传入泛型 + +> 4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。 + +用法:`obj.add<泛型类型>()` + +在不传泛型时云对象方法返回的类型为`Promise`,传入泛型后callFunction返回的类型为`Promise<泛型类型>` + +**代码示例** + +```ts +// 云对象todo代码 +'use strict'; +module.exports = { + async add(title, content) { + return { + errCode: 0, + errMsg: '', + detail: `Todo added, title: ${title}, content: ${content}` + } + }, +} +``` + +```ts +// 客户端代码 +const todo = uniCloud.importObject('todo') +type CallObjectResult = { + errCode : number + errMsg : string + detail : string +} +todo.add('todo title', 'todo content').then((res) => { + const detail = res.detail // res类型为CallObjectResult,可直接通过.detail访问其中detail属性 + console.log(detail) +}).catch((err : any | null) => { + console.error(err) +}) +``` \ No newline at end of file