Sunday, October 15, 2017

I saw Daniel Rosenwasser speak at AngularMix!

scriptsharp and GWT were given as examples of trying to solve the problem TypeScript solves once upon the time. The former lets you write stuff in C# and then render out JavaScript with it and the latter is the same thing with the C# part just swapped out with Java and herein GWT is pronounced "gwit" and stands for Google Web Toolkit. It really wasn't until CoffeeScript came along that human beings had the Aha! moment of realizing that we could just make a new language that compiled to JavaScript in lieu of some terrible translator. Of course, everyone hates CoffeScript, so something new was needed. That brings us to Daniel Rosenwasser, the Microsoft Program Manager managing TypeScript who, yes, was the one giving this history lesson as history is written by the victors.

His talk, following that opening, was a smorgasbord of nuggets of information on TypeScript such as:

  • Contravariance exists! The image above shows off a change coming to TypeScript with regards to strictFunctionTypes. If Dog extends Animal, you may not (one day) hand in an array of Animal at a function signature that expects an array of Dog. However, this is a new restriction coming that does not immediately exist in TypeScript. An example of where contravariance is allowed materializes when a type defined as a union has an instance which gets eventually more strictly defined as one of the classes in the union. Daniel typed up an example of this which I didn't quite retain in my notes, but a good paraphrasing might be:
    class Cat{
       speak():void {
          alert("Meow");
       }
    }
    class Dog{
       speak():void {
          alert("Bark");
       }
    }
    type Animal = Cat | Dog;
    let contravariance = function (animal:Animal):void {
       let dog = (animal as Dog);
       dog.speak();
    }
    contravariance(new Dog());
  • The term "narrowing" came up in another, different example that had a union in it, and one I can recreate verbatim, which started out like so:
    function sendUpdates(emailAddr: string | string[]) {
    Somewhere in the function we have some branching logic around the possibilities of the union like this:
    if (Array.isArray(emailAddr)) {
    This sort of use of control flow analysis is given the moniker: narrowing
  • Somewheres between verbatim and paraphrasing I offer up this quote: "There are two types of programming languages, those that people badmouth and those that people don't use."
  • .dts files are a set of declarations for what exists at runtime. Under the hood, these tell TypeScript about what your JavaScript looks like at runtime making, I would suspect, some of the compiler safety and Intellisense around that feasible.
  • A ts-ignore suppression comment looks like so:
    let x: number;
    //@ts-ignore;
    x = "hello";

    ...and does what you think it does. The comment is decorating the line immediately below it and keeping the compiler from objecting.
  • The Elvis operator in TypeScript is akin to the safe navigation operator in C#. You may dot, dot, dot out to something confident in your ability to break Demeter's Law (and open Pandora's Box) provided that you lead the dots with question marks. You just end up with a null assignment should something break somewhere along the chain. Have fun troubleshooting that! (Only fools rush in.)

No comments:

Post a Comment