Giter Site home page Giter Site logo

typed-we-app's Introduction

⚠ ARCHIVED since tencent finally got their own offical type definitions for WeApp

so goodbye and thanks for all the contributors

typed-we-app

TypeScript declaration file for WeApp API

NPM

Install

  • With npm installed

    npm install typed-we-app --save-dev
  • With typings installed

    typings install github:Emeryao/typed-we-app -SG
  • Or get the declaration file here and include it to your project

About

Based on the official WeApp API documentation
Wechat Web DevTool Version 1.01.1712150
WAService.js Version 1.7.0

Sample

  • TypeScript
wx.request({
    url: 'request/url',
    success: (res) => {
        console.log(res.data);
    }
});

let ctx = wx.createCanvasContext();
ctx.fillText('hello', 0, 0);

let actions = ctx.getActions();
wx.drawCanvas({
    canvasId: 'canvas-id',
    actions
});

wx.getBLEDeviceServices({
    deviceId: '',
    success: (res) => {
        console.log(res.services[0].uuid);
    },
});

Last Update

2019.05.19

typed-we-app's People

Contributors

cxon626 avatar emeryao avatar goumang2010 avatar jhcao23 avatar liny4cn avatar sutext avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

typed-we-app's Issues

How to return never when code uploadFile task onProgressUpdate callback function

function uploadFile(url: string, filePath: string, progress?: (progress: number) => never):Promise<{id:number,errMsg?:string}> {
    return new Promise<{id:number,errMsg?:string}>((resolve, reject) => {
        const uploadTask = wx.uploadFile({
            url: url,
            filePath: filePath,
            name: 'resourceFile',
            success: res => resolve(res),
            fail: e => reject(e)
        });
        if (progress){
        uploadTask.onProgressUpdate =
            (res: { progress: number; totalBytesSent: number; totalBytesExpectedToSend: number; }): never => {
                return progress(res.progress)
            }
        }
    });
}

function updateFileUploadProgress(progress:number):never{
    // do something
    return never
}
async function post2sever(page: IPage, images: { path: string }[]) {
    try {
        let fileIDs:Array<number> = new Array<number>()
        for (let img of images) {
            let res = await uploadFile('/resource', img.path, updateFileUploadProgress)
            if (res.errMsg) throw new Error(`upload image[${img.path}] occurs error:${res.errMsg}`)
            fileIDs.push(res.id);
        }
    } catch{

    }
}

bellow function how to retun never? must throw an error or use while true?

function updateFileUploadProgress(progress:number):never{
    // do something
    return never
}

Report an error when declaring a variable with this.data

make a sample page.js

// @ts-check

Page({
  data: {
    pageTop: -1,
    pageBottom: 0,
  },
  onLoad() {
    const { pageTop, pageBottom } = this.data;
    console.log(pageTop, pageBottom);
  },
});

It reports an error Type 'Object' has no property 'pageTop' and no string index signature.

image

Go to file index.d.ts line 53, change the data type from Object to any will mute the error.

interface PageParam {
        /**页面的初始数据 */
        data?: Object;

Is there a bug or I missed some configuration?

suggestion to merge to official site

Hi Emery,

I searched around Internet and found your definition is the best and the most updated!
Will you consider to merge this great work to the official typescript definition DefinitelyTyped collection: DefinitelyTyped, so that more developers could know your excellent work and contribute together?

thanks,
John

默认的lib,应该只包含 es2016 , 不应该包含DOM

当跟wx一块工作的时候, tsconfig.json 里面应该只包含 lib:["es2016"] , 因为所有的DOM API 在微信中都不可用

interface Console 却包含在 dom 中, 所以 we-app.d.ts 里面应该包含 Console 的定义, 另外 setTimeout setInterval clearInterval 也需要定义,
下面是我写的定义, Console 是从 dom.d.ts 里面 偷的 :P


interface Console {
    assert(condition?: boolean, message?: string, ...data: any[]): void;
    clear(): void;
    count(label?: string): void;
    debug(message?: any, ...optionalParams: any[]): void;
    dir(value?: any, ...optionalParams: any[]): void;
    dirxml(value: any): void;
    error(message?: any, ...optionalParams: any[]): void;
    exception(message?: string, ...optionalParams: any[]): void;
    group(groupTitle?: string, ...optionalParams: any[]): void;
    groupCollapsed(groupTitle?: string, ...optionalParams: any[]): void;
    groupEnd(): void;
    info(message?: any, ...optionalParams: any[]): void;
    log(message?: any, ...optionalParams: any[]): void;
    markTimeline(label?: string): void;
    profile(reportName?: string): void;
    profileEnd(): void;
    table(...tabularData: any[]): void;
    time(label?: string): void;
    timeEnd(label?: string): void;
    timeStamp(label?: string): void;
    timeline(label?: string): void;
    timelineEnd(label?: string): void;
    trace(message?: any, ...optionalParams: any[]): void;
    warn(message?: any, ...optionalParams: any[]): void;
}

declare var console: Console;

/**
 * 定期执行特定的处理程序
 * @param handler 处理程序
 * @param waitTime 等待的时间
 */
declare function setTimeout(handler: () => any, waitTime: number): number;

/**
 * 每隔一段时间执行特定的处理程序
 * @param handler 处理程序
 * @param waitTime 每次执行间隔的时间
 */
declare function setInterval(handler: () => any, waitTime: number): number;

/**
 * 清理由 setInterval 产生的 timer
 * @param timerId timer 的id
 */
declare function clearInterval(timerId: number);

/**
 * 清理由 setTimeout 产生的timer
 * @param timerId timer 的 id
 */
declare function clearTimeout(timerId: number);

declare function require(lib: string): any;

setImmediateclearImmediate 我没定义, 主要是没用到,也不确定 小程序支持

interface UpdateManager 方法返回值未定义

报错信息

node_modules/@types/we-app/index.d.ts(1062,9): error TS7010: 'onCheckForUpdate', which lacks return-type annotation, implicitly has an 'any' return type.
node_modules/@types/we-app/index.d.ts(1063,9): error TS7010: 'onUpdateFailed', which lacks return-type annotation, implicitly has an 'any' return type.
node_modules/@types/we-app/index.d.ts(1064,9): error TS7010: 'onUpdateReady', which lacks return-type annotation, implicitly has an 'any' return type.

修改代码后通过

interface UpdateManager {
        applyUpdate: Function,
        onCheckForUpdate(callback: Function):any,
        onUpdateFailed(callback: Function):any,
        onUpdateReady(callback: Function):any
    }

Example

Hi @emeryao,

Do you have an example about how to use interface Page in coding?

Thanks,
John

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.