Closed alonesuperman closed 5 years ago
目前我的解决方案是在sfc.d.ts里写
type ApiOption = Omit<Partial<RequestOptions>, "success" | "fail" | "complete">;
interface Uni {
request(options: ApiOption): Promise<any>;
}
完美运行
补充 interface NodesRef下的boundingClientRect返回的其实是SelectorQuery 但是d.ts里写成了void 导致没法继续链式调用exec
应改为
boundingClientRect(callback?: (result: NodeInfo) => void): SelectorQuery ;
boundingClientRect的问题@dcloudio/types内已经修正,Promise 暂未统一处理,开发者先自行处理。
关于api 的 Promise 化类型提示。除 uni.request 有返回值
、同步的方法(即以 sync 结束)
、以 create 开头的方法
、以 manager 结束的方法
、返回 task 的 api
暂不处理外,其他可以升级 @dcloudio/types@3.3.0 试试看
部分 api promise 化提示已支持,后续进度请跟踪 #3009 查看
问题描述 typescript开发的时候 uni.request({...}).then 报错 大致就是没法推断request返回的是Promise
打开uni.d.ts可以看到 uni.request方法写的是
request(options?: RequestOptions): RequestTask; 这其实不对根据文档描述
如果没有传入 success / fail / complete 参数,则会返回封装后的 Promise 对象
所以至少可以增加一个重载(抛砖引玉,肯定有其它写法)
如果ts版本较低可以自行实现垫片
For versions of TypeScript at or above 3.5
In TypeScript 3.5, the
Omit
type was added to the standard library. See examples below for how to use it.For versions of TypeScript below 3.5
In TypeScript 2.8, the
Exclude
type was added to the standard library, which allows an omission type to be written simply as:For versions of TypeScript below 2.8
You cannot use the
Exclude
type in versions below 2.8, but you can create a replacement for it in order to use the same sort of definition as above. However, this replacement will only work for string types, so it is not as powerful asExclude
.系统信息:
发行平台: 任意
操作系统: 任意
HBuilderX版本: 未使用
uni-app版本 Environment Info:
System: OS: Windows 10 CPU: (4) x64 Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz Binaries: Node: 8.15.0 - D:\Program Files\nodejs\node.EXE Yarn: 1.16.0 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.8.0 - D:\Program Files\nodejs\npm.CMD Browsers: Edge: 42.17134.1.0 npmPackages: @dcloudio/types: => 0.2.6 @dcloudio/uni-app-plus: => 0.0.247 @dcloudio/uni-app-plus-nvue: 0.0.1 @dcloudio/uni-app-plus-nvue-v8: 0.0.1 @dcloudio/uni-cli-shared: => 0.2.972 @dcloudio/uni-h5: => 0.6.3 @dcloudio/uni-mp-alipay: => 0.0.821 @dcloudio/uni-mp-baidu: => 0.0.850 @dcloudio/uni-mp-qq: => 0.0.105 @dcloudio/uni-mp-toutiao: => 0.0.345 @dcloudio/uni-mp-weixin: => 0.0.966 @dcloudio/uni-template-compiler: => 0.9.180 @dcloudio/vue-cli-plugin-hbuilderx: latest => 1.0.120 @dcloudio/vue-cli-plugin-uni: latest => 0.9.493 @dcloudio/webpack-uni-mp-loader: => 0.3.639 @dcloudio/webpack-uni-nvue-loader: 0.0.1 @dcloudio/webpack-uni-pages-loader: => 0.2.852 @vue/babel-helper-vue-jsx-merge-props: 1.0.0 @vue/babel-plugin-transform-vue-jsx: 1.0.0 @vue/babel-preset-app: 3.9.2 @vue/babel-preset-jsx: 1.0.0 @vue/babel-sugar-functional-vue: 1.0.0 @vue/babel-sugar-inject-h: 1.0.0 @vue/babel-sugar-v-model: 1.0.0 @vue/babel-sugar-v-on: 1.0.0 @vue/cli-overlay: 3.9.0 @vue/cli-plugin-babel: 3.5.1 => 3.5.1 @vue/cli-plugin-typescript: ^3.5.1 => 3.9.0 @vue/cli-service: ^3.9.0 => 3.9.2 @vue/cli-shared-utils: 3.9.0 @vue/component-compiler-utils: 2.6.0 @vue/preload-webpack-plugin: 1.1.0 @vue/web-component-wrapper: 1.2.0 mpvue-page-factory: 1.0.1 mpvue-template-compiler: 1.0.13 uni-h5-vue: 2.6.10 uni-mp-vue: 2.6.10 vue: ^2.6.10 => 2.6.10 vue-class-component: ^6.3.2 => 6.3.2 (7.1.0) vue-hot-reload-api: 2.3.3 vue-loader: 15.7.0 vue-property-decorator: ^8.2.1 => 8.2.1 vue-router: 3.0.1 vue-style-loader: 4.1.2 vue-template-compiler: ^2.6.10 => 2.6.10 vue-template-es2015-compiler: 1.9.1 vuex: ^3.0.1 => 3.1.1 npmGlobalPackages: @vue/cli: Not Found (我不知道为什么找不到,实际版本是3.8.4)
设备信息 任意