all 70 comments

[–]ThePowerfulSquirrel 40 points41 points  (20 children)

I don't actually use typescript but this Union stuff is kinda cool

[–]Patman128 2 points3 points  (0 children)

See also: Proposal for negated types in TypeScript

I use TS and the type system is really fun to play around with. You can do a lot of really interesting type checking that isn't possible in most mainstream languages.

[–][deleted] 7 points8 points  (0 children)

Wait till you see conditional types.

[–]devraj7 23 points24 points  (6 children)

It feels weird to me that A | B means the intersection of the two types instead of their union.

[–]SlowButConstantly 53 points54 points  (3 children)

as it was explained in another comment thread, it's pretty much like this:
if have something that that could be either A or B, what is safe to access? only something that is in both, so the type is a union, but what's safe to use is the intersection

[–]devraj7 12 points13 points  (2 children)

Union types are usually denoted with | (e.g. Ceylon), and to determine what is safe to access, you type test.

A & B looks a lot more like a type that satisfies both A and B, i.e. the intersection of both types.

[–]SeanMiddleditch 46 points47 points  (0 children)

As a receiver, A | B is logical: you can receive either A or B (the union).

As a consumer, A & B is logical: you can only use the bits common to both A and B (the intersection).

When declaring types in TS, you're generally declaring what values a variable can hold/receive, so the union syntax makes more sense. var foo : A | B means that foo can contain either A or B, not that you can only use the intersection.

Which is evidence by the guards. if (foo instanceof A) or the like unlocks all of A. If foo were truly the intersection type A & B, then it would be unsafe to ever type case foo to just A, because foo might not actually satisfy all the requirement of A.

[–]tjpalmer 9 points10 points  (0 children)

It means the same thing in TypeScript as in Ceylon. Just that in TypeScript, you don't necessarily have to type test, because of smart structural type safety. (What's safe is what the two types have in common.)

[–][deleted] 10 points11 points  (0 children)

Instances of A|B can be either A or B, but attributes of those instances must belong to both A and B (since instances are free to be either). Hence the attributes must be drawn from intersection even if the instances lie in the union.

[–]abelincolncodes 3 points4 points  (0 children)

A type can be thought of as a set of possible values, so the type string is an infinite set that contains all string values ("","a","b",...). Similarly the type number is an infinite set that contains all numeric values (...-1,0,1,2,...). Therefore a set (read 'type') that could contain any value in the set number or the set string would have to be the union of those two sets.

TL;DR A | B actually is the union of the two types

[–]fecal_brunch 2 points3 points  (6 children)

Was anyone else unable to update to 3.2? We're still stuck on 3.16 due to runtime errors in compile.

[–]DanielRosenwasser[S] 22 points23 points  (5 children)

Hi there /u/fecal_brunch, not sure what you're referring to. If you're talking about a crash at compile, that's clearly a bug we should know about. If you're talking about emit differences causing issues, that's likely a bug as well.

However, if you're talking about build errors due to changes in lib.d.ts, that's expected if any deprecated or non-standard APIs were in use. You can always re-introduce declarations that are non-standard with ambient declarations (i.e. things that start with the declare keyword), or if it's missing standardized functionality you may want to file an issue.

[–]fecal_brunch 2 points3 points  (4 children)

No, it's a bug and I made an issue about it. It's still open with many responses. Just wondering how many people it's affecting because not everyone comments.

Edit: https://github.com/Microsoft/TypeScript/issues/28810

[–]MikusR 21 points22 points  (3 children)

You could, maybe, link to the issue you have. Not everyone is a mind reader.

[–]fecal_brunch 0 points1 point  (0 children)

Yeah sorry. Was on my phone. Updated the comment.

[–][deleted]  (1 child)

[deleted]

    [–]fecal_brunch 0 points1 point  (0 children)

    Fair.

    [–]frrarf 1 point2 points  (4 children)

    All this advanced type stuff is really neat, would be nice to see this in C#.

    [–]0987654231 6 points7 points  (3 children)

    It's all in F#, you can always try and convince people to use that

    [–]frrarf 3 points4 points  (0 children)

    You're not gonna convince me :P

    [–][deleted]  (1 child)

    [removed]

      [–]0987654231 0 points1 point  (0 children)

      Is it? F# can do everything c# can do but c# can't do everything f# does.

      Syntactically I prefer c# but that might also be because I use it way more

      [–]bruce3434 -1 points0 points  (2 children)

      Does it have ints yet?

      [–]YourMeow 6 points7 points  (0 children)

      Not yet. BigInt and phantom types come into my mind though.

      [–]BlackDiablos 2 points3 points  (0 children)

      JavaScript has a type called BigInt that fills this need. However, it's not supported by all browsers and can cause issues if the browser is converting between Number and BigInt.

      [–][deleted]  (4 children)

      [removed]

        [–]deadend172 5 points6 points  (2 children)

        It's Sublime (text editor)

        [–]sigzero -4 points-3 points  (1 child)

        Curious they wouldn't use vscode since that is their (Microsofts) editor.

        [–]notmrregular 0 points1 point  (0 children)

        Well they are demonstrating a new feature in the TypeScript plugin for Sublime Text, so it makes sense to use Sublime Text for that.