Live data from Hacker News

TypeScript 3.5

devblogs.microsoft.com

41–50 of 88 posts

Re: TypeScript 3.5

#41
post #2

I would really love to enjoy TypeScript, especially now that they've added HKTs, but I am constantly running into type errors when the types clearly match, leading to weird workaround code where the type of some key in some object is specified as: false | 'x' | 'y' | undefined Meaning passing in `'x'` should work just fine. But instead, it specializes it to just a string, then throws a type error. The workaround requ…

> especially now that they've added HKTs

Hmm .. as far as I'm aware, they haven't added this yet: https://github.com/microsoft/TypeScript/issues/1213

Note that higher-kinded types (though I'd rather call it "higher-kinded polymorphism" or something—type-level functions are not really types) would allow code similar to this:

  function foo(f: (v: A) => F, v: A): F {
      return f(v);
  }

Re: TypeScript 3.5

#42
post #39

Earlier quoted context omitted.

> 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 no…

> 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.

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

#43

Earlier quoted context omitted.

So the example of `false | 'x' | 'y' | undefined` turning into string is invalid? I feel like I'm missing something.

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 declara…

Didn't realize "as const" worked; will have to try that next time.

Re: TypeScript 3.5

#44
It sounds like type inference for object literals is coming along, even if there a few quibbles about some of the defaults on literals mentioned here.

Did they ever get a solution in place for partial function application (e.g. - as for Ramda.js)?

When this gets to the point where the only place I have to define explicit types is for JSON data read from the network, I’ll consider using it.

I know I’m in the minority, but I’d rather NOT have any type checking than have to read through Java-esque drivel (at least most modern languages put the types after the identifier like Pascal does, rather than before like C). In practice, I don’t spend much time chasing type errors, but do spend too much time reading through MEGO inducing verbiage which I would just as soon not.

Re: TypeScript 3.5

#45
post #2

I would really love to enjoy TypeScript, especially now that they've added HKTs, but I am constantly running into type errors when the types clearly match, leading to weird workaround code where the type of some key in some object is specified as: false | 'x' | 'y' | undefined Meaning passing in `'x'` should work just fine. But instead, it specializes it to just a string, then throws a type error. The workaround requ…

My biggest hope is that one day they will make type error formatting way, way, way better in VSCode. There are some gnarly type errors that take a lot of concentration to work out from that tiny, unformatted error text box :|

Re: TypeScript 3.5

#46

Earlier quoted context omitted.

> Objectively, it simply isn't, obviously To be fair, TS has some pretty bad unsoundness problems, especially regarding co/contra variance in generics, and for example some annoying weirdness with number/string keys in objects. Still, the type system is one of the most amazing ones I know.

Yeah but if you think about it they are putting a straight jacket on a madman while a committee organizes regular PCP shipments to said madman. That they got it to work amazes me, that they got it to work well astounds me. It's one of the few parts of my stack that I interact with every day I don't occasionally want to hunt down the creators of.

> It's one of the few parts of my stack that I interact with every day I don't occasionally want to hunt down the creators of.

Reactive Extensions was definitely one of those pieces of stacks.

Re: TypeScript 3.5

#47
post #39

Earlier quoted context omitted.

> 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 no…

> 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.

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

#48
post #2

I would really love to enjoy TypeScript, especially now that they've added HKTs, but I am constantly running into type errors when the types clearly match, leading to weird workaround code where the type of some key in some object is specified as: false | 'x' | 'y' | undefined Meaning passing in `'x'` should work just fine. But instead, it specializes it to just a string, then throws a type error. The workaround requ…

> 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 no…

> 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

#49
post #23
post #5

Typescript 3.5: fixing the problems we introduced and providing workarounds for new features in a broken type system.

I can't downvote you but I want to point out that the only thing you accomplish with such a comment is coming off as ignorant and bitter. I have no idea why on Earth you think the type system is broken. Objectively, it simply isn't, obviously, but I'd love to see you trip over your own feet trying to argue that it is. Moreover, it's honestly remarkable how they've managed to retrofit such an amazing type system on to…

I don't know if 3.5 fixes this, but here's an example from 3.4

    function messUpTheArray(arr: Array): void {
      arr.push(3);
    }

    const strings: Array = ['foo', 'bar'];
    messUpTheArray(strings);

    const s: string = strings[2];
    console.log(s.toLowerCase())
You obviously can't push a number into an Array, yet typescript allows it.

For my money, ReasonML is a better solution for type issues (hindley-milner types are awesome).

Re: TypeScript 3.5

#50
post #42
post #39

Earlier quoted context omitted.

> 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.

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')

Is this not what enums with associated values are?
Post reply on HN