miniprogram-api-promise icon indicating copy to clipboard operation
miniprogram-api-promise copied to clipboard

能支持typescript 吗

Open yytm opened this issue 5 years ago • 6 comments

yytm avatar Nov 06 '19 09:11 yytm

自定义

// definition.d.ts 放在 src 根目录即可
declare module 'miniprogram-api-promise' {
  type ParamType<T> = T extends (option: infer P) => any ? P : T
  type SuccessCallback<T> = NonNullable<NonNullable<ParamType<T>>['success']>
  type ResultType<T> = ParamType<SuccessCallback<T>>

  type Wx = WechatMiniprogram.Wx

  type WxAsyncFunctionKeys = {
    [K in keyof Wx]: NonNullable<ParamType<Wx[K]>> extends { success?: any } ? K : never
  }[keyof Wx]

  type Wxp = {
    [P in WxAsyncFunctionKeys]: (option: ParamType<Wx[P]> = {}) => Promise<ResultType<Wx[P]>>
  }

  export declare function promisify<Func>(func: Func): (option: ParamType<Func> = {}) => Promise<ResultType<Func>>
  export declare function promisifyAll(wx: Wx, wxp: Wxp): void
}

listenzz avatar Dec 11 '19 08:12 listenzz

@listenzz 你的代码直接用不了,很多报错。 image

MoonBall avatar Dec 30 '19 09:12 MoonBall

@MoonBall 我这边没问题哦

我们用的版本:

"typescript": "^3.5.3",
"miniprogram-api-typings": "^2.9.3",

我们 tsconfig.json

{
  "compilerOptions": {
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "target": "es5",
    "lib": ["ES2016"],
    "module": "es6",
    "strict": true,
    "moduleResolution": "node",
    "typeRoots": ["./node_modules/@types", "./node_modules/miniprogram-api-typings"],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "downlevelIteration": true
  },
  "exclude": ["node_modules", "dist", "webpack.config.js", "jest.config.js"]
}

截屏2019-12-30下午6 47 51

listenzz avatar Dec 30 '19 10:12 listenzz

@listenzz 请问引用之后该如何定义新的wx对象? image

helloforrestworld avatar Apr 23 '21 10:04 helloforrestworld

@helloforrestworld

const wxp = {} as Wxp

listenzz avatar Apr 23 '21 11:04 listenzz

@listenzz 感谢回复 我导出了Wxp可以了 image

helloforrestworld avatar Apr 23 '21 11:04 helloforrestworld