News for hackers. Stuff that matters.
Re:Love for TypeScript by paradite on Saturday July 30, 2016 @04:10AM (#12192107)
I have not used TypeScript, so can anyone explain to me what is the difference between the above the normal JavaScript code like this:

    function myFunction(x) {
        if (x === null) { 
            return;
        }

        console.log(x + 1); // x is now a number
    }
Edit: Or is this example being used to show that TypeScript now support multiple types including null in the parameter?

I assumed that such feature would be available by default.

Re:Love for TypeScript by topspin on Saturday July 30, 2016 @04:55AM (#12192223)
I haven't used Typescript either, but some of the differences are self evident to me. myFunction(x) can be called with any argument type; tooling can't detect an inappropriate type and output a warning/error(?), so you get to find certain mistakes the hard way; at runtime. "foo"+1 might make sense, but "foo"/1 is garbage, silently propagating 'undefined' up the stack in conventional Javascript, costing time and money for everyone. The user of myFunction(n: number | null) is provided with a meaningful signature during development, this saves time by providing precise information in a concise manner. Those of us that have spent time with statically typed languages and good tooling know what a blessing this is. The Typescript variant offers the possibility of a performance optimization; x+1 might compile down to a single instruction, as opposed to whatever runtime type check has to be invoked when the type of x isn't/can't be known at compile time. I don't know if that optimization is 'real' in practice given our contemporary javascript/typescript stack, but you can bet it will be eventually.
Re:Love for TypeScript by ycitm on Saturday July 30, 2016 @04:25AM (#12192152)
You could call your js function with a string, and rather than a compile time error you'd get broken behaviour at runtime.
Re:Love for TypeScript by Too on Saturday July 30, 2016 @04:37AM (#12192182)
Inside the function you also have to continue using x as a number, like you wouldnt be able to accidentally pass it into a function accepting only string as argument etc.
Re:Love for TypeScript by adam12 on Saturday July 30, 2016 @07:39AM (#12192563)
your js version:

y = myFunction(1); // y = 2

y = myFunction("1"); // y = "11"

the ts version wouldn't allow you to compile myFunction("1")

Re:Love for TypeScript by mckoss on Saturday July 30, 2016 @10:08AM (#12193023)
Except for a few cases where Typescript let's you transpile ES6 syntax to JavaScript ES5, the compiler does not change your code; it just checks it for type errors.

The point of the example is that Typescript recently added support for non-null types, but is flexible enough to analyze where types have been further restricted by an early function return.