Live data from Hacker News

Announcing TypeScript 2.1

blogs.msdn.microsoft.com

171–180 of 226 posts

Re: Announcing TypeScript 2.1

#171

Can someone comment on the difference in reliability between using typescript and a natively statically typed language like haskell or scala? Is there any? Or is the type safety really as good when you use ts

Haskell and Scala are safer than TypeScript... but not by too much :) The only major safety feature lacking in TypeScript are variance annotations and proper variance for functions, which are bivariant WRT their arguments. Short demonstration of the problem:

  // this should not be assignable, functions should be
  // contravariant WRT arguments, so if o2 is subtype of o1
  // then F1 is subtype of F2, which means you can't assign
  // a value of type F2 to F1: there are functions in the
  // set of functions F2 that don't belong in the subset F1
  type O1 = { a: string; b: string }
  type O2 = { a: string; b: string; c: string } 
  type F1 = (o: O1) => string
  type F2 = (o: O2) => string

  // should not compile!
  let f: F1 = (o: O2) => { return o.c.toString() }

  f({a: '1', b: '2'}) // throws at run time (o.c undefined)
Other than that, TypeScript has been pretty safe since the addition of "strictNullChecks".

Haskell and Scala have algebraic datatypes (or case classes) which make it easier to model data (especially in Haskell where the syntax is really nice). TypeScript also supports disjoint unions (union of several types with a tag field that has a concrete string value as the type) and control flow analysis, so with some effort it can provide "idiomatic" JS-style ADTs. Haskell and Scala can do exhaustiveness checks, making sure you've covered all the cases. TS can also sort of do that (when strictNullChecks are on):

  type T1 = { tag: 't1', value: string }
  type T2 = { tag: 't2', value: number }
  type T3 = { tag: 't3' }

  type T = T1 | T2 | T3

  function f(t: T):string {
    switch (t.tag) {
      case 't1': return t.value;
      case 't2': return t.value.toString()
      // Compiles only if you uncomment this line.
      // Otherwise inferred return type string|undefined is
      // incompatible with specified return type string
      //case 't3': return 'N/A'
    }
  } 
Not as pleasant or as general as Haskell, but better than most other mainstream languages.

Both Haskell and Scala have higher kinded generics, something that TypeScript still lacks. This means importing category theory concepts in TypeScript is pretty much impossible. The problem isn't just with category theory though; the need to parameterise generic types with other generic types as type arguments can come up everyday JS code too. For example, a library that accepts a promise constructor as an argument is parameterized by a generic type [1]

Before v2.1 TypeScript already had some powerful features for working with record types, such as record unions and intersections. With mapped types introduced in 2.1 however, you could say it surpasses Haskell and Scala in this regard - if not with features, then at least with pleasantness / ease of use. I believe its possible to achieve the same things in Haskell with vinyl [2][3], but those are definitely not Haskell's native records. Not sure if Scala's shapeless [4] can provide something similar (the answer is probably yes).

Haskell has type classes, and Scala can provide the "equivalent" via implicits+traits. Both of these allow a very nice and generic style of programming with implementation flexibility (you can write new typeclass instances for old code). TypeScript has structural interfaces, which offer some flexibility compared to nominal interfaces: you can write an interface that is a subset of old code, then use both old and new code under that interface without modifying the old code. Still, its not as flexible as typeclasses (the implementation must already be a subset of the original old code). They also can't offer the return-type-based implementation selection that typeclasses can.

Of course, Haskell and Scala both have various type / macro / template superpowers that TypeScript doesn't have and isn't likely to ever have. For everyday programming though, the above list should cover the vast majority of interesting features.

[1]: https://github.com/Microsoft/TypeScript/issues/1213 [2]: https://hackage.haskell.org/package/vinyl-0.5.2/docs/Data-Vi... [3]: https://hackage.haskell.org/package/vinyl-0.5.2/docs/Data-Vi... [4]: https://github.com/milessabin/shapeless/wiki/Feature-overvie...

Re: Announcing TypeScript 2.1

#172
What's the future of TypeScript in Visual Studio (not Code)?

I've got a project that just silently fails to build in VS - no errors, no warnings, build successful - but no output.

It seems like the Typescript tools inside VS2015 (even with the latest update) just aren't ready.

Re: Announcing TypeScript 2.1

#173
post #120

Earlier quoted context omitted.

> The first time you feel the speed/confidence of refactoring with accurate 'Find usages', you'll decide if the undeniable overhead of types is worth it. For your information: in VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed. Edit and disclaimer: Not sure why I'm getting downvoted, my comment doesn't contradict parent message. I personally type my code too (w…

If the code isn't typed, then you can't find all references accurately. For example, in the below, you've no idea if the "a" inside the function is the same is as on "foo". ```javascript var foo = { a: true, // Find all references on "a" here... b: "hello" }; foo.a = false; bar(foo); function bar(obj) { obj.a = false; // Won't find this "a". } ``` Call site inference can follow this sometimes. However with types it c…

Question: How close is Flow coming within VS Code to parity with the features available to TypeScript?

Re: Announcing TypeScript 2.1

#174
post #93

Earlier quoted context omitted.

As a long-time .NET dev... Thank You for Async/Await in TypeScript.

async/await in C# is pretty nasty and usually overused and overhyped for nothing but downside. It screws debugging, it screws call stacks, it dictates you to write bad code and it's just plain complicated. It infects your code up and down the stack and every new method MS now releases seems to be .SomethingAsync(). I likened it today to having a garden hose in a Victorian sewer but then declaring the sewer is the bot…

I agree mostly. One of the biggest issues with async/await in C# is that if not used carefully you can get deadlocks. If you get even one such deadlock, the cost of debugging it can wipe out all your hardware savings. For that reason most people should not use async/await in C#.

Since there is no deadlock issue in JavaScript async/await should result in code that is easier to write as well as maintain.

Re: Announcing TypeScript 2.1

#175

If you still haven't given TypeScript a go as a Javascripter, now is a great time to do so. Whether you end up adopting it or not, it's interesting to get the types out of your mind and into the code. The first time you feel the speed/confidence of refactoring with accurate 'Find usages', you'll decide if the undeniable overhead of types is worth it.

> The first time you feel the speed/confidence of refactoring with accurate 'Find usages', you'll decide if the undeniable overhead of types is worth it. For your information: in VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed. Edit and disclaimer: Not sure why I'm getting downvoted, my comment doesn't contradict parent message. I personally type my code too (w…

VSCode has become a mind-blowingly good editor. It's closing in on the power of heavy-weight IDEs with much better performance than some code editors (e.g. Atom), even.

Re: Announcing TypeScript 2.1

#176
post #35

Earlier quoted context omitted.

"Typing isn't a magic bullet, ..., but it sure makes development easier and your apps more stable." That right there is the whole essence of typed programming :)

Going one level deeper, the underlying truism is that computers make better bean counters than people, even programmers. Type systems are good insofar as they free up cognitive resources of the programmer. Conversely, type systems are bad when they make programmers burn attention resources without enough return. (This is a difficult thing for people to evaluate, because sometimes the "return" from a type system can p…

In my opinion, the critical aspect of typed code is the ability to declare a variable as either a number, string or binary. Let me declare this up front, let the compiler enforce it, and my mind is freed from semi-paranoid concerns about edge cases and spurious inputs.

But sometimes I don't care exactly what sort of number is used. This is where many type systems go overboard, by forcing me pick a 64 bit unsigned integer or whatever.

Re: Announcing TypeScript 2.1

#178

TypeScript is great. I built https://www.findlectures.com over a year, starting in plain Javascript. Once the codebase was large enough that got stuck I added TypeScript, and it's been great for isolating defects. It's nice paired with React (vs PropTypes) because the checking happens a lot earlier and is much richer.

Nice site. I know what I will be doing tonight, when I arrive home. Thanks :)

Re: Announcing TypeScript 2.1

#179
post #82

Earlier quoted context omitted.

> For your information: in VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed. I didn't downvote, but this seems incorrect to me. I'm sure that it works in small code bases, but once your code gets large enough (and dynamic enough) it's going to have to start missing things.

Why would their algorithm stop to work if your code gets large? It's not some kind of magic. :) My current project has 10,000 LOC and 130 files. If I use "Rename symbol" on something imported in different files of my project, the editor will open all the files, edit all the names and just wait for me to save the changes.

> Why would their algorithm stop to work if your code gets large? It's not some kind of magic. :)

Their algorithm always fails on certain cases. If your code base is larger, it's more likely to contain those cases.

Example case that will always fail:

    const x = { foo: () => {} }
    x["f" + "oo"]();
Now rename foo to bar.

Yes, this is a contrived case, but we had similar code in production at my last job.

Re: Announcing TypeScript 2.1

#180

If you still haven't given TypeScript a go as a Javascripter, now is a great time to do so. Whether you end up adopting it or not, it's interesting to get the types out of your mind and into the code. The first time you feel the speed/confidence of refactoring with accurate 'Find usages', you'll decide if the undeniable overhead of types is worth it.

Can anyone weigh in on TypeScript vs Elm?

Other people's comments explain the details well, but I wanted to say that I have a large (real-world) project where the bulk of the app and all the business logic is in Elm, and all the more fiddly UI bits are in Typescript. The two languages communicate with each other over typesafe Elm ports and it all works fairly wonderfully.
Post reply on HN