typescript編寫(xiě)微信小程序創(chuàng)建項(xiàng)目的方法
創(chuàng)建項(xiàng)目
在微信開(kāi)發(fā)者工具創(chuàng)建項(xiàng)目,在語(yǔ)言中選擇 TypeScript
改造項(xiàng)目
編輯 package.json
文件,修改 miniprogram-api-typings
和 typescript
版本
{ "name": "miniprogram-ts-quickstart", "version": "1.0.0", "description": "", "scripts": { "compile": "./node_modules/typescript/bin/tsc", "tsc": "node ./node_modules/typescript/lib/tsc.js" }, "keywords": [], "author": "", "license": "", "dependencies": { }, "devDependencies": { "typescript": "^4.1.3", "miniprogram-api-typings": "^2.12.1-beta.0" } }
編輯 tsconfig.json 文件, 修改 lib 為 ["esnext"],支持最新語(yǔ)法, 刪除 typeRoots 配置項(xiàng)
{ "compilerOptions": { "strictNullChecks": true, "noImplicitAny": true, "module": "CommonJS", "target": "ES5", "allowJs": false, "experimentalDecorators": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "inlineSourceMap": true, "inlineSources": true, "noFallthroughCasesInSwitch": true, "noUnusedLocals": true, "noUnusedParameters": true, "strict": true, "removeComments": true, "pretty": true, "strictPropertyInitialization": true, "lib": ["esnext"] }, "include": [ "./**/*.ts" ], "exclude": [ "node_modules" ] }
執(zhí)行 npm install
刪除項(xiàng)目下 typings 目錄, 的 復(fù)制 node_modules 下 miniprogram-api-typings 的 types 文件到項(xiàng)目根目錄
在 miniprogram 下創(chuàng)建 interface 目錄并創(chuàng)建 IAppOption.ts 文件,最后編輯 app.ts 文件,
// IAppOption.ts export default interface IAppOption { globalData: { text: string; } } // app.ts import IAppOption from "./interface/IAppOption"; App<IAppOption>({ globalData: { text: "Hello,Word!" }, onLaunch() { } })
在 詳細(xì) -> 本地設(shè)置 -> 調(diào)試基礎(chǔ)庫(kù),直接選擇最新的
使用 Promise 化的微信小程序api
以前可以通過(guò) miniprogram-api-promise
這個(gè)包來(lái)完成 api Promise 化,或者自己寫(xiě)
現(xiàn)在可以直接使用,比如 wx.getStorageInfo ,在 lib.wx.api.d.ts
中返回了 PromisifySuccessResult
PromisifySuccessResult
返回了Promise
getStorageInfo<TOption extends GetStorageInfoOption>( option?: TOption ): PromisifySuccessResult<TOption, GetStorageInfoOption> type PromisifySuccessResult< P, T extends AsyncMethodOptionLike > = P extends { success: any } ? void : P extends { fail: any } ? void : P extends { complete: any } ? void : Promise<Parameters<Exclude<T['success'], undefined>>[0]>
兩種用法,大多數(shù)api都支持
wx.getStorageInfo({ success: () => { console.log('成功執(zhí)行') }, fail: () => { console.log('失敗執(zhí)行') }, complete: () => { console.log('接口調(diào)用結(jié)束') } }) wx.getStorageInfo().then(() => { console.log('成功執(zhí)行') }).catch(() => { console.log('失敗執(zhí)行') }).finally(() => { console.log('接口調(diào)用結(jié)束') })
源碼: https://github.com/NikolasSky/ts-miniprogram/tree/master/ts-miniprogram-base
到此這篇關(guān)于typescript編寫(xiě)微信小程序創(chuàng)建項(xiàng)目的方法的文章就介紹到這了,更多相關(guān)typescript開(kāi)發(fā)微信小程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 前端深入理解Typescript泛型概念
- 詳解JavaScript私有類字段和TypeScript私有修飾符
- JS裝飾者模式和TypeScript裝飾器
- 詳解TypeScript中的類型保護(hù)
- TypeScript在React項(xiàng)目中的使用實(shí)踐總結(jié)
- Vue3+TypeScript封裝axios并進(jìn)行請(qǐng)求調(diào)用的實(shí)現(xiàn)
- TypeScript 安裝使用及基本數(shù)據(jù)類型
- 詳解Typescript里的This的使用方法
- TypeScript泛型參數(shù)默認(rèn)類型和新的strict編譯選項(xiàng)
相關(guān)文章
javascript下有關(guān)dom以及xml節(jié)點(diǎn)訪問(wèn)兼容問(wèn)題
javascript下有關(guān)dom以及xml節(jié)點(diǎn)訪問(wèn)兼容問(wèn)題...2007-11-11實(shí)現(xiàn)圖片首尾平滑輪播(JS原生方法—節(jié)流)
下面小編就為大家?guī)?lái)一篇實(shí)現(xiàn)圖片首尾平滑輪播(JS原生方法—節(jié)流)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Js實(shí)現(xiàn)累加上漂浮動(dòng)畫(huà)示例
這篇文章主要為大家介紹了Js實(shí)現(xiàn)累加上漂浮動(dòng)畫(huà)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11一個(gè)仿Windows UI的html table,兼容IE和firefox
兼容IE和firefox的仿Windows UI的html table2008-11-11javascript的propertyIsEnumerable()方法使用介紹
propertyIsEnumerable()是用來(lái)檢測(cè)屬性是否屬于某個(gè)對(duì)象的,如果檢測(cè)到了,返回true,否則返回false,下面為大家介紹下其具體的使用2014-04-04JavaScript?中的?parseInt()?函數(shù)詳解
parseInt函數(shù)將其第一個(gè)參數(shù)轉(zhuǎn)換為一個(gè)字符串,對(duì)該字符串進(jìn)行解析,然后返回一個(gè)整數(shù)或?NaN,這篇文章主要介紹了JavaScript?的?parseInt()?函數(shù),需要的朋友可以參考下2023-05-05