typescript - 类型'number'的参数不能赋值给'string'类型的参数

时间:2017-01-03 00:14:05

标签: typescript

我无法弄清楚如何使这个函数不会产生编译器错误。

该函数以ms为单位持续时间,如果数字小于10,则以0为前提小时/分钟/秒。

    $token_url = 'https://discordapp.com/api/oauth2/token';

    if (!$code)
    {
        echo '<a href="'. $auth_url . '">Login with Discord</a>';
    } else {
        // cURL Request Goes Here.
        $chB = curl_init();
        $post_opts = array(
            'client_id'     =>  '[id_goes_here]',
            'client_secret' =>  '[secret_goes_here',
            'code'          =>  $code,
            'redirect_uri'  =>  '[valid_redirect_uri_goes_here]',
            'grant_type'    =>  'authorization_code',
        );

        $c_opts = array(
            CURLOPT_URL     =>  $token_url,
            CURLOPT_POST    =>  true,
            CURLOPT_RETURNTRANSFER  =>  true,
            CURLOPT_HTTPHEADER  =>  array(
                'Content-type: application/x-www-form-urlencoded',
            ),
            CURLOPT_POSTFIELDS  =>  $post_opts,
        );

        curl_setopt_array($chB, $c_opts);

        $run_now = curl_exec($chB);
        $code = curl_getinfo($chB, CURLINFO_HTTP_CODE);
        curl_close($chB);

        print($code);
        var_dump($run_now);
    }

4 个答案:

答案 0 :(得分:2)

尝试以下代码和编译器不会返回任何错误。

private msToTime(duration:any):string {
    var milliseconds:string = String((duration%1000)/100)
    var seconds:string = String((duration/1000)%60)
    var minutes:string = String((duration/(1000*60))%60)
    var hours:string = String((duration/(1000*60*60)%24)

    hours   = (parseInt(hours) < 10)   ? "0" + hours   : hours;
    minutes = (parseInt(minutes) < 10) ? "0" + minutes : minutes;
    seconds = (parseInt(seconds) < 10) ? "0" + seconds : seconds;

    return hours + ":" + minutes + ":" + seconds;
}

答案 1 :(得分:0)

您尝试调用parseInt((duration%1000)/100),其中(duration%1000)/100已经是int,因此您可以放弃此方法,并使用

var milliseconds:string = String((duration%1000)/100)
var seconds:string = String((duration/1000)%60)
var minutes:string = String((duration/(1000*60))%60)
var hours:string = String((duration/(1000*60*60))%24)

答案 2 :(得分:0)

{{1}}接受字符串输入。出于您的目的,请使用{{1}},

答案 3 :(得分:-1)

parseInt函数类型的参数是字符串 所以,你可以这样做: var milliseconds:string = String(parseInt((duration%1000)/ 100).toString());