Monday, July 11, 2016

Using TypeScript feels like you are in the Matrix, you can bend things(types) at will

Using TypeScripts feels like you are in the Matrix, you can bend things(types) at will.


Improved the code from this..


export function pageBuilder(req: express.Request, res: express.Response, next: Function)
{

    let dRequest = denodeify(request);

    let cookie = request.cookie(req.headers["cookie"]);

    let headers = {cookie};

    let parts = {
        header : dRequest({url: "http://localhost:168/ui/header.html", headers}),
        toolbox: dRequest({url: "http://localhost:168/ui/toolbox.html", headers}),
        footer : dRequest({url: "http://localhost:168/ui/footer.html", headers})
    };


    Q.all(<any>parts).then(result =>
    {
     
        let html = buildPage(result.header.body, result.toolbox.body, result.footer.body);

        res.send(html);
    });

}

..to this:


export function pageBuilder(req: express.Request, res: express.Response, next: Function)
{

    let dRequest = denodeify(request);

    let cookie = request.cookie(req.headers["cookie"]);

    let headers = {cookie};

    let parts = {
        header : dRequest({url: "http://localhost:168/ui/header.html", headers}),
        toolbox: dRequest({url: "http://localhost:168/ui/toolbox.html", headers}),
        footer : dRequest({url: "http://localhost:168/ui/footer.html", headers})
    };


    Q.all(<any>parts).then(result =>
    {
        let partsResult: {
            header: express.Request,
            toolbox: express.Request,
            footer: express.Request
        } = <any>result;


        let html = buildPage(partsResult.header.body, partsResult.toolbox.body, partsResult.footer.body);

        res.send(html);
    });

}

The current signature of Q's TypeScript definition does not allow dictionary, but you can do adhoc'y types even in the code's body, don't necessarily need to put the type in the global context.


Happy Coding!

No comments:

Post a Comment