News for hackers. Stuff that matters.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by thomasahle on Wednesday November 19, 2014 @03:51PM (#8632535)

    var x = [1, 'world']; // x: Array<string|number>
I'm not sure I wouldn't prefer this to give an error.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by tomp on Wednesday November 19, 2014 @05:54PM (#8633227)
That's the main issue with advanced type systems that feature things like union types, generics/polymorphism, subtyping... It's hard to specify the line between type inference guessing correctly what the programmer wants, and type inference now catching a programmer's bug. This is the reason that most Hindley-Milner type systems (e.g. OCaml's) don't allow recursive types (which come up e.g. for typing the Y combinator).

Another example is:

    function equal<T>(lhs: T, rhs: T): boolean {
      return lhs === rhs;
    }

    var e = equal(42, 'hello');
Here, there obviously is a type for `T`: `any` (the toplevel dynamic type) or `number|string`.

This problem is caused by the requirement that type inference is "complete", i.e. that it correctly infers types for all programs that have a valid typing (i.e. that would work if the programmer specified all types).

Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by Retra on Wednesday November 19, 2014 @06:03PM (#8633268)
This makes me wonder what the point of writing 'var' is if it doesn't tell you the type of the variable.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by UberMouse on Wednesday November 19, 2014 @08:02PM (#8633802)
So variables are explicitly declared, don't have issues of mis typing a variable name and accidentally declaring a new one.

Note: This is just my thinking, I don't know if it's the actual reason

Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by Retra on Thursday November 20, 2014 @03:20PM (#8637905)
That's a sensible reason, sure, but I'd rather declare it with an actual type instead.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by thomasahle on Thursday November 20, 2014 @12:06PM (#8636744)
Yes, for complicated things like this, explicitly declaring the type is probably preferable.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by tomp on Thursday November 20, 2014 @04:32AM (#8634979)
As opposed to doing what?
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by thomasahle on Thursday November 20, 2014 @12:04PM (#8636732)
Like python, just `e = equal(...`
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by tomp on Thursday November 20, 2014 @12:24PM (#8636851)
Python had to go through a lot of pain for that, the "nonlocal" keyword was only added in Python 3.0, and the local/global distinction is very fragile.

Javascript and Coffeescript try to solve this in different ways, most people think that JavaScript's method sucks, and many are not fond of CoffeeScript's non-locality. Personally, I think that demanding variable declaration is still the best option (possibly except for interactive sessions).

Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by lpw25 on Thursday November 20, 2014 @06:27AM (#8635228)
Actually OCaml does support recursive types. Recursion through objects and polymorphic variants is always supported, and full recursive types can be turned on with the `rectypes` command-line option.
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by Horusiath on Wednesday November 19, 2014 @05:24PM (#8633076)
Why? This is exactly what type definition describes - an array of string or numbers. If you'd like to have an either array of strings or array of numbers, you'd type: Array<string>|Array<number>
Re:TypeScript 1.4 sneak peek: union types, type guards, and ... by frowaway001 on Wednesday November 19, 2014 @05:43PM (#8633175)
I think he meant that he would prefer not having to deal with an array where types are all over the place.