News for hackers. Stuff that matters.
Re:TypeScript 3.5 by phiresky on Wednesday May 29, 2019 @03:50PM (#20044575)
> in many cases being unable to import a JS library in that doesn't include typedefs

Make sure you have "allowJs": true in your tsconfig.json. Otherwise, you'll have to create a minimal file with `declare module "xyz";`

> I fail to see how TS can be considered a superset of JS

You have to interpret all TS "errors" as warnings for that to be true (which they mostly are considering tsc still emits code regardless of non-syntax-error errors)

> I just cannot take a type system seriously when workarounds like that are needed.

Your problem really just seems to be that the type inference does not have magical powers. There is no other programming language that has union types as powerful as TypeScript (as in discriminating by whatever you want), but yeah when you do `return {x: 1, y: 2}` the compiler doesn't always know whether you mean to return an object where the type of x is `1` or where it is `number`. In some cases it could be better still, but doing this in general is just not possible.

Re:TypeScript 3.5 by shakna on Wednesday May 29, 2019 @07:43PM (#20046888)
> There is no other programming language that has union types as powerful as TypeScript

Ada's record [0] type stands out. From what I can see, TS' unions are not nearly as powerful.

[0] https://en.m.wikibooks.org/wiki/Ada_Programming/Types/record

Re:TypeScript 3.5 by phillipcarter on Wednesday May 29, 2019 @09:02PM (#20047361)
> There is no other programming language that has union types as powerful as TypeScript

I suggest looking into Idris and other languages with dependent types. Nothing against typescript, but it’s not even close.

Re:TypeScript 3.5 by logicchains on Wednesday May 29, 2019 @10:06PM (#20047721)
Idris doesn't have anonymous sum types, nor does Coq or Lean. Which language were you thinking of?
Re:TypeScript 3.5 by phillipcarter on Thursday May 30, 2019 @11:20AM (#20052113)
Ceylon comes to mind; haven't googled others.
Re:TypeScript 3.5 by ElFitz on Wednesday May 29, 2019 @05:37PM (#20045807)
> There is no other programming language that has union types as powerful as TypeScript

How about Swift and OCaml? ReasonML comes to mind too. Haven't tried actively comparing any of them though.

Re:TypeScript 3.5 by tomp on Wednesday May 29, 2019 @06:06PM (#20046086)
OCaml/Reason have discriminated unions, which means that you have to explicitly specify which variant of the union you're using, such as

  type union = Bool of bool | Char of char | Nothing

  let (x, y, z) = (Nothing, Bool true, Char 'x')
Re:TypeScript 3.5 by saagarjha on Wednesday May 29, 2019 @08:44PM (#20047265)
Is this not what enums with associated values are?
Re:TypeScript 3.5 by tomp on Thursday May 30, 2019 @02:02AM (#20048783)
Probably... but in any case TypeScript also supports naked (edit: I just made up the name) unions, where you don't need to declare them upfront, and where values of different types can be included directly. Such as:

  let a = random() ? 'a' : 1
TypeScript infers type

  string | number
Re:TypeScript 3.5 by marton78 on Thursday May 30, 2019 @06:54AM (#20049936)
This is still a discriminated union, only that the discriminator is provided by Javascript and is thus hidden: it's `typeof`.
Re:TypeScript 3.5 by tomp on Thursday May 30, 2019 @08:17AM (#20050387)
Yeah, that's the runtime perspective... but for the type system, they're different... with the "naked" union being much harder (as they're non-algebraic - `a | b | b` always equals `a | b` and might even equal just `a` for some values of `a` and `b`)
Re:TypeScript 3.5 by spion on Thursday May 30, 2019 @12:09PM (#20052632)
No, this works on any overlapping types too - even unsound combinations such as `T | Array<T>`
Re:TypeScript 3.5 by ElFitz on Friday May 31, 2019 @03:34AM (#20058896)
Interesting! Thanks!
Re:TypeScript 3.5 by kbp on Wednesday May 29, 2019 @06:58PM (#20046581)
What features are you thinking of in those languages? I don't know Swift but I can't think of anything comparable in OCaml.
Re:TypeScript 3.5 by ElFitz on Friday May 31, 2019 @03:41AM (#20058932)
Sorry; I don't have much of an answer here.

I merely know from experience that they have a pretty good type system, but not much more. So I asked :-)

Answers have been enlightening

Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:00PM (#20044692)
This doesn't need magic powers. All you have to do is treat literal values inside type descriptions differently.
Re:TypeScript 3.5 by phiresky on Wednesday May 29, 2019 @04:09PM (#20044796)
Inside of type descriptions, literal values are always treated as literals. The problem only appears when you declare a variable _without_ an explicit type like `const foo = {k: 'x'}`, which the compiler then infers to be of type `foo: {k: string}` and not `foo: {k: 'x'}`.

Both of these are valid inferences, and it's impossible to know which one the user wants in the general case, which is why you have to explicitly say `const foo = {k: 'x' as const}`.

Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:34PM (#20045111)
So the example of `false | 'x' | 'y' | undefined` turning into string is invalid?

I feel like I'm missing something.

Re:TypeScript 3.5 by phiresky on Wednesday May 29, 2019 @04:45PM (#20045247)
here's a full example. If you do this:

    function test(p: {k: false | 'x' | 'y' | undefined}) {
        // do stuff
    }
    const p = {k: 'x'} // inferred type: {k: string}
    //...
    test(p); // type error : Type 'string' is not assignable to type 'false | "x" | "y" | undefined'
    
The problem is that the type of p.k is inferred to `string` not `x`. The compiler can't know this is fine because you could call `foo(p)` in between the declaration and the `test` call, that mutates p.k.

If you add `as const` or an explicit type to p it is fine.

(I think this is what the GGP was referring to)

Re:TypeScript 3.5 by Rapzid on Wednesday May 29, 2019 @06:10PM (#20046127)
Didn't realize "as const" worked; will have to try that next time.
Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:54PM (#20045347)
Thanks!