【问题标题】:vuex) how to Not all code paths error in typescript?vuex)如何在打字稿中并非所有代码路径错误?
【发布时间】:2022-08-16 04:45:05
【问题描述】:

也许我忘记了这个函数的返回类型。但我不知道如何适应它。首先保持我原来的功能从迁移到打字稿 请分享你的答案:)

const arrayPromise: Promise<string[]> = new Promise<string[]>((resolve, reject) => {
                // requset element : depAirportId, arrAirportId, depPlandTime // chose certain airline : &airlineId=AAR
                axios.get(url)
                    .then((res:any) => {
                        // ! Not all code paths return a value.ts(7030)
                        const item = res.data.response.body.items.item
                        this.state.totalCount = res.data.response.body.totalCount
                        if(this.state.totalCount < 1)
                        return this.state.noTicket

                        this.state.depTime = res.data.response.body.items.item.forEach((obj:any) => {
                            /* 보간법을 이용하면 function 가능 (object에 간섭을 안하므로 가능) */
                            obj.depTime = `${obj.depPlandTime}`.slice(-4, -2)
                            /* Object.values : make every Object to Array */
                            return Object.values(obj)
                        })
                        this.state.depMin = res.data.response.body.items.item.forEach((obj:any) => {
                            obj.depMin = `${obj.depPlandTime}`.slice(-2)
                            return Object.values(obj)
                        })
                        this.state.arrTime = res.data.response.body.items.item.forEach((obj:any) => {
                            obj.arrTime = `${obj.arrPlandTime}`.slice(-4, -2)
                            return Object.values(obj)
                        })
                        this.state.arrMin = res.data.response.body.items.item.forEach((obj:any) => {
                            obj.arrMin = `${obj.arrPlandTime}`.slice(-2)
                            return Object.values(obj)
                        })
  • 你需要resolve(ret); return; 而不是return ret;。但是如果你只是res = await axios.get...你可以摆脱new Promise,使用return并且不要在错误的情况下被遗忘。
  • 谢谢回答!我会尝试你的建议。
  • 对不起。您能提供更多详细信息吗?

标签: typescript vuex typescript-typings


【解决方案1】:

不知道这段代码是否对您有意义,因为您没有提供有关返回的 json 的信息,因此很难弄清楚您实际上想要什么。但我认为它应该对你有所帮助。

看看描述原因的cmets,我做了修改……你的代码对我来说没有太大意义,尤其是string[]的返回值。我退还您从电话中获得的物品。如果您需要一个字符串 [],您应该定义该字符串数组中的内容。

class YourClass {

state: any;

// use async functions in typescript
async arrayPromise(): Promise<any[]> { // i could not figure out what you want to return, so i return the items you parse in your function
    try {
        const result: any[] = [];

        // requset element : depAirportId, arrAirportId, depPlandTime // chose certain airline : &airlineId=AAR
        // ! url was undefined
        const url = 'https://';
        // call request and await the response (this is the way .this() is not the way, because it is just another callback hell)
        const res =  await axios.get(url);

        // ! do you realy need to modify this.state in this function?
        this.state.totalCount = res.data.totalCount;

        if (this.state.totalCount < 1) { // ! always use brackets (avoids fat-finger bugs like 'goto fail')
            // return this.state.noTicket; // ? ooh ... don't know what you want to return here, i guess an empty array.
            return result;
        }

        // ! don't use .forEach, because it does a callback on each element
        for (let obj of res.data.items) { // should iterate over youre Items ... 
            // parse some data and enrich the object
            obj.depTime = `${obj.depPlandTime}`.slice(-4, -2);
            obj.depMin = `${obj.depPlandTime}`.slice(-2);
            obj.arrTime = `${obj.arrPlandTime}`.slice(-4, -2);
            obj.arrMin = `${obj.arrPlandTime}`.slice(-2);

            // append the object to the result array
            result.push(obj.depTime);
        })
        // the additional forEach where superficial

        // Finally return the result.
        return result;
    } catch (error: any) {
        console.error('Error occurred', error);
        throw error;
        // you can also `return [];` if you do not want to do error handling in the calling method
    }
}

}

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2013-07-01
    • 2014-04-16
    • 2015-02-07
    • 2012-04-16
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多