From a23ae6c63cd949e6e98eed7b3b28221ff88c6a6b Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Tue, 12 Jun 2018 22:22:34 +0800 Subject: [PATCH 1/2] Add typing for wepy --- types/wepy/app.d.ts | 13 ++ types/wepy/base.d.ts | 12 ++ types/wepy/component.d.ts | 20 ++ types/wepy/event.d.ts | 6 + types/wepy/index.d.ts | 35 ++++ types/wepy/mixin.d.ts | 1 + types/wepy/page.d.ts | 23 +++ types/wepy/tsconfig.json | 27 +++ types/wepy/tslint.json | 3 + types/wepy/util.d.ts | 15 ++ types/wepy/wepy-tests.ts | 41 ++++ types/wepy/wx_enhanced.d.ts | 391 ++++++++++++++++++++++++++++++++++++ 12 files changed, 587 insertions(+) create mode 100644 types/wepy/app.d.ts create mode 100644 types/wepy/base.d.ts create mode 100644 types/wepy/component.d.ts create mode 100644 types/wepy/event.d.ts create mode 100644 types/wepy/index.d.ts create mode 100644 types/wepy/mixin.d.ts create mode 100644 types/wepy/page.d.ts create mode 100644 types/wepy/tsconfig.json create mode 100644 types/wepy/tslint.json create mode 100644 types/wepy/util.d.ts create mode 100644 types/wepy/wepy-tests.ts create mode 100644 types/wepy/wx_enhanced.d.ts diff --git a/types/wepy/app.d.ts b/types/wepy/app.d.ts new file mode 100644 index 0000000000..cc2636ed92 --- /dev/null +++ b/types/wepy/app.d.ts @@ -0,0 +1,13 @@ +export interface AppConfig { + appEvents?: string[]; + pageEvents?: string[]; + noPromiseAPI?: string[] | { [name: string]: boolean }; +} + +export interface AppConstructor { + new (): app; +} + +export default class app { + $init(wepy: any, config: AppConfig): any; +} diff --git a/types/wepy/base.d.ts b/types/wepy/base.d.ts new file mode 100644 index 0000000000..91c934b8b1 --- /dev/null +++ b/types/wepy/base.d.ts @@ -0,0 +1,12 @@ +import page, { PageConstructor } from "./page"; +import app, { AppConstructor, AppConfig } from "./app"; + +declare const defaultExport: { + $createApp?: (appClass: AppConstructor, appConfig: AppConfig) => app; + $createPage?: ( + pageClass: PageConstructor, + pagePath: string | boolean + ) => page; +}; + +export default defaultExport; diff --git a/types/wepy/component.d.ts b/types/wepy/component.d.ts new file mode 100644 index 0000000000..ddcea63ed6 --- /dev/null +++ b/types/wepy/component.d.ts @@ -0,0 +1,20 @@ +import event from "./event"; + +export default class component { + $isComponent: boolean; + $prefix: string; + data: { [name: string]: any }; + + computed?: { [name: string]: (self?: component) => any }; + methods?: { [name: string]: (evt?: event) => any }; + + $init($wxpage: any, $root: any, $parent: any): void; + $initMixins(): void; + onLoad(): void; + setData(k: string | string[], v: any): void; + getWxPage(): any; + $setIndex(index: number): void; + $getComponent(com: any): any; + $apply(fn: () => void): void; + $nextTick(fn: () => void): void; +} diff --git a/types/wepy/event.d.ts b/types/wepy/event.d.ts new file mode 100644 index 0000000000..f3a0d6c5a8 --- /dev/null +++ b/types/wepy/event.d.ts @@ -0,0 +1,6 @@ +export default class event { + active: boolean; + constructor(name: string, source: any, type: any); + $destroy(): void; + $transfor(wxevent: any[]): void; +} diff --git a/types/wepy/index.d.ts b/types/wepy/index.d.ts new file mode 100644 index 0000000000..3be681c10e --- /dev/null +++ b/types/wepy/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for wepy 1.7 +// Project: https://github.com/Tencent/wepy#readme +// Definitions by: Jiayu Liu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +import component from "./component"; +import mixin from "./mixin"; +import event from "./event"; +import page from "./page"; +import app from "./app"; +import util from "./util"; +import base from "./base"; +import { WxEnhances } from "./wx_enhanced"; + +interface DefaultExport extends WxEnhances { + event: typeof event; + app: typeof app; + component: typeof component; + page: typeof page; + mixin: typeof mixin; + $createApp: typeof base.$createApp; + $createPage: typeof base.$createPage; + $isEmpty: typeof util.$isEmpty; + $isEqual: typeof util.$isEqual; + $isDeepEqual: typeof util.$isDeepEqual; + $has: typeof util.$has; + $extend: typeof util.$extend; + $isPlainObject: typeof util.$isPlainObject; + $copy: typeof util.$copy; +} + +declare const defaultExport: DefaultExport; + +export default defaultExport; diff --git a/types/wepy/mixin.d.ts b/types/wepy/mixin.d.ts new file mode 100644 index 0000000000..c70aedd7b1 --- /dev/null +++ b/types/wepy/mixin.d.ts @@ -0,0 +1 @@ +export default class mixin {} diff --git a/types/wepy/page.d.ts b/types/wepy/page.d.ts new file mode 100644 index 0000000000..7655621106 --- /dev/null +++ b/types/wepy/page.d.ts @@ -0,0 +1,23 @@ +import component from "./component"; + +export interface UrlParam { + url: string; +} + +export interface PageConstructor { + new (): page; +} + +export default class page extends component { + $preloadData: { [key: string]: any }; + $init(wxpage: any, $parent: any): any; + $route( + type: "redirectTo" | "navigateTo", + url: string | UrlParam, + params?: { [name: string]: any } + ): any; + $preload(key: string | { [key: string]: any }, data: any): any; + $switch(url: string | UrlParam): any; + $redirect(url: string, params?: object): any; + $back(delta: number | { delta: number }): any; +} diff --git a/types/wepy/tsconfig.json b/types/wepy/tsconfig.json new file mode 100644 index 0000000000..f363a749a1 --- /dev/null +++ b/types/wepy/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "app.d.ts", + "base.d.ts", + "component.d.ts", + "event.d.ts", + "index.d.ts", + "mixin.d.ts", + "page.d.ts", + "util.d.ts", + "wx_enhanced.d.ts", + "wepy-tests.ts" + ] +} diff --git a/types/wepy/tslint.json b/types/wepy/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/wepy/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/wepy/util.d.ts b/types/wepy/util.d.ts new file mode 100644 index 0000000000..cff1be1ce5 --- /dev/null +++ b/types/wepy/util.d.ts @@ -0,0 +1,15 @@ +declare const defaultExport: { + $isEmpty: (obj: object) => boolean; + $isEqual: (a: any, b: any, aStack?: any[], bStack?: any[]) => boolean; + $isDeepEqual: (a: any, b: any, aStack?: any[], bStack?: any[]) => boolean; + $has: (obj: object, path: string) => boolean; + $extend: () => any; + $copy: (obj: T, deep?: boolean) => T; + $isPlainObject: (obj: any) => boolean; + $resolvePath: (route: string, url: string) => string; + $getParams: (url: string) => object; + hyphenate: (str: string) => string; + camelize: (str: string) => string; +}; + +export default defaultExport; diff --git a/types/wepy/wepy-tests.ts b/types/wepy/wepy-tests.ts new file mode 100644 index 0000000000..9ed5462fb9 --- /dev/null +++ b/types/wepy/wepy-tests.ts @@ -0,0 +1,41 @@ +import wepy from "wepy"; + +export class MyComponent extends wepy.component { + data = { + reveal: false, + img: "", + animationData: "", + imgClassName: "", + imgMode: "scaleToFill", + title: "loading", + titleClassName: "" + }; + + methods = {}; + + onLoad() { + super.onLoad(); + } +} + +export class BindJwc extends wepy.page { + config = { + navigationBarTitleText: "校历", + enablePullDownRefresh: true + }; + mixins = []; + data = {}; + computed = { + termName(): number { + return 1; + } + }; + methods = { + returnToday(): number { + return 123; + } + }; + + init() {} + async onLoad() {} +} diff --git a/types/wepy/wx_enhanced.d.ts b/types/wepy/wx_enhanced.d.ts new file mode 100644 index 0000000000..d5e66dea34 --- /dev/null +++ b/types/wepy/wx_enhanced.d.ts @@ -0,0 +1,391 @@ +export interface WechatProfileDetails { + photoFilePath?: string; + nickName?: string; + lastName?: string; + middleName?: string; + firstName?: string; + remark?: string; + mobilePhoneNumber?: string; + email?: string; + url?: string; + workAddressCountry?: string; + workAddressState?: string; + workAddressCity?: string; + workAddressStreet?: string; + workAddressPostalCode?: string; + homeFaxNumber?: string; + homePhoneNumber?: string; + homeAddressCountry?: string; + homeAddressState?: string; + homeAddressCity?: string; + homeAddressStreet?: string; + homeAddressPostalCode?: string; +} + +export interface RotateAnimation { + rotate(deg: number): Animation; + rotateX(deg: number): Animation; + rotateY(deg: number): Animation; + rotateZ(deg: number): Animation; + rotate3d(): Animation; +} + +export interface ScaleAnimation { + scale(sx: number): Animation; + scaleX(sx: number): Animation; + scaleY(sy: number): Animation; + scaleZ(sz: number): Animation; + scale3d(): Animation; +} + +export interface TranslateAnimation { + translate(tx: number): Animation; + translateX(tx: number): Animation; + translateY(ty: number): Animation; + translateZ(tz: number): Animation; + translate3d(): Animation; +} + +export interface SkewAnimation { + skew(ax: number): Animation; + skewX(ax: number): Animation; + skewY(ay: number): Animation; +} + +export interface Animation + extends RotateAnimation, + ScaleAnimation, + SkewAnimation, + TranslateAnimation { + opacity(x: number): Animation; + backgroundColor(x: string): Animation; + width(x: number): Animation; + height(x: number): Animation; + top(x: number): Animation; + left(x: number): Animation; + bottom(x: number): Animation; + right(x: number): Animation; +} + +export interface LagLng { + latitude: number; + longitude: number; +} + +export type CallbackFunction = ( + callbacks: { + success: (res: T) => void; + fail: () => void; + complete: () => void; + } +) => void; + +export interface MapContext { + getCenterLocation: CallbackFunction; + + moveToLocation(): void; + + translateMarker(params: { + markerId: number; + autoRotate: boolean; + duration: number; + destination: LagLng; + animationEnd: () => void; + }): void; + + includePoints(params: { padding: number[]; points: LagLng[] }): void; + + getRegion: CallbackFunction<{ southwest: LagLng; northeast: LagLng }>; + + getScale: CallbackFunction<{ scale: number }>; +} + +export interface UserInfo { + nickName: string; + avatarUrl: string; + gender: string; + city: string; + province: string; + country: string; + language: string; +} + +export interface UrlParam { + url: string; +} + +export interface FilePathParam { + filePath: string; +} + +// WePY enhanced Wx interfaces and methods below are all accessible +export interface WxEnhances { + addPhoneContact(param: WechatProfileDetails): Promise; + + authorize(param: { scope: string }): Promise; + + canIUse(name: string): Promise; + + canvasGetImageData( + params: { + canvasId: string; + x: number; + y: number; + width: number; + height: number; + }, + context: any + ): Promise<{ + width: number; + height: number; + data: Uint8ClampedArray; + }>; + + canvasPutImageData( + params: { + canvasId: string; + data: Uint8ClampedArray; + x: number; + y: number; + height?: number; + wdith: number; + }, + context: any + ): Promise; + + canvasToTempFilePath( + params: { + x: number; + y: number; + width: number; + height: number; + destWidth: number; + destHeight: number; + canvasId: string; + }, + context: any + ): Promise<{ tempFilePath: string }>; + + checkIsSoterEnrolledInDevice(params: { + checkAuthMode: "fingerPrint" | "facial" | "speech"; + }): Promise<{ + isEnrolled: boolean; + errMsg: string; + }>; + + checkIsSupportSoterAuthentication(): Promise<{ + supportMode: string[]; + }>; + + checkSession(): Promise; + + chooseInvoiceTitle(): Promise<{ + type: "0" | "1"; + title: string; + taxNumber: string; + companyAddress: string; + telephone: string; + bankName: string; + bankAccount: string; + errMsg: string; + }>; + + chooseLocation(): Promise<{ + name: string; + address: string; + latitude: number; + longitude: number; + }>; + + chooseVideo(params: { + sourceType: string[]; + compressed: boolean; + maxDuration: number; + }): Promise<{ + tempFilePath: string; + duration: number; + size: number; + height: number; + width: number; + }>; + + clearStorage(): Promise; + + createAnimation(params: { + duration: number; + timingFunction: string; + delay: number; + transformOrigin: string; + }): Promise; + + createCanvasContext(canvasId: string, context: any): Promise; + + createMapContext(mapId: string, context: any): MapContext; + + createSelectorQuery(): Promise<{}>; + + getLocation(params: { + type: string; + altitude: boolean; + }): Promise<{ + latitude: number; + longitude: number; + speed: number; + accuracy: number; + altitude: number; + verticalAccuracy: number; + horizontalAccuracy: number; + }>; + + getNetworkType(): Promise<{ networkType: string }>; + + getSavedFileInfo( + params: FilePathParam + ): Promise<{ + errMsg: string; + size: number; + createTime: number; + }>; + + getSavedFileList(): Promise<{ + errMsg: string; + fileList: object[]; + }>; + + getSetting(): Promise<{ + authSetting: { + "scope.userInfo": boolean; + "scope.userLocation": boolean; + "scope.address": boolean; + "scope.invoiceTitle": boolean; + "scope.werun": boolean; + "scope.record": boolean; + "scope.writePhotosAlbum": boolean; + "scope.camera": boolean; + }; + }>; + + getShareInfo(params: { + shareTicket: string; + timeout: number; + }): Promise<{ + errMsg: string; + encryptedData: string; + iv: string; + }>; + + getSystemInfo(): Promise<{ + brand: string; + model: string; + pixelRatio: number; + screenWidth: number; + screenHeight: number; + windowWidth: number; + windowHeight: number; + statusBarHeight: number; + language: string; + version: string; + system: string; + platform: string; + fontSizeSetting: string; + SDKVersion: string; + }>; + + getUserInfo(params: { + withCredentials: boolean; + lang: string; + timeout: number; + }): Promise<{ + userInfo: object; + rawData: string; + signature: string; + encryptedData: string; + iv: string; + }>; + + hideLoading(): Promise; + + hideNavigationBarLoading(): Promise; + + makePhoneCall(params: { phoneNumber: string }): Promise; + + navigateTo(params: UrlParam): Promise; + + onUserCaptureScreen(): Promise; + + openLocation(params: { + latitude: number; + longitude: number; + scale?: number; + name?: string; + address?: string; + }): Promise; + + pageScrollTo(params: { + scrollTop: number; + duration: number; + }): Promise; + + redirectTo(params: UrlParam): Promise; + + removeSavedFile(params: FilePathParam): Promise; + + removeStorage(params: { key: string }): Promise; + + request(params: UrlParam): Promise; + + requestPayment(params: { + timeStamp: string; + nonceStr: string; + package: string; + signType: string; + paySign: string; + }): Promise; + + scanCode(params: { + onlyFromCamera?: boolean; + scanType?: string[]; + }): Promise<{ + result: string; + scanType: string; + charSet: string; + path: string; + }>; + + setNavigationBarAlpha(params: { alpha: number }): Promise; + + setNavigationBarColor(params: { color: number }): Promise; + + setNavigationBarTitle(params: { title: string }): Promise; + + setStorage(params: { key: string; data: string | object }): Promise; + + showActionSheet(params: { + itemList: string[]; + itemColor: string; + }): Promise; + + showLoading(params: { title: string }): Promise; + + showModal(params: { + title: string; + content: string; + showCancel?: boolean; + cancelText?: string; + cancelColor?: string; + confirmText?: string; + confirmColor?: string; + }): Promise<{ confirm: boolean; cancel: boolean }>; + + showNavigationBarLoading(): Promise; + + showToast(params: { + title: string; + icon?: "success" | "loading" | "none"; + image?: string; + duration?: number; + mask?: boolean; + }): Promise; + + switchTab(params: UrlParam): Promise; +} From 8a0e97c93ffdc0d471c53d9fad2d4e63ecdd5e2c Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Wed, 13 Jun 2018 14:52:43 +0800 Subject: [PATCH 2/2] lower typescript version --- types/wepy/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/wepy/index.d.ts b/types/wepy/index.d.ts index 3be681c10e..bff673ee8c 100644 --- a/types/wepy/index.d.ts +++ b/types/wepy/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/Tencent/wepy#readme // Definitions by: Jiayu Liu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 +// TypeScript Version: 2.2 import component from "./component"; import mixin from "./mixin";