4 ms·
How so? It looks a lot like a less-rich Java, whose type system has existed for literally decades. In fact, the generics syntax and type erasure was directly in
by cgh 3y ago
How so? It looks a lot like a less-rich Java, whose type system has existed for literally decades. In fact, the generics syntax and type erasure was directly influenced by Java.
- coev 3y agoMore like indirectly influenced by Java by way of C# (Anders Hejlsberg designed both C# and TS)
- ildjarn 3y agoTypescript has far more powerful type constructs than Java.
- pionar 3y agoI disagree. The language was probably more inspired by C#, which is itself inspired by Java. Type erasure is just part of transpiling to JS.
- acchow 3y agoTho Typescript does have structural types, mapped types, union types, optional types, type aliases..... Bunch of useful type features.
- skybrian 3y agoI haven’t used Java in many years, but TypeScript’s structural types give it a different flavor than I remember from Java. Unlike in Java, most of the types I define aren’t classes or interfaces. It’s just data that has an expected shape. Check out the design of Zod, for example. I have a bunch of Zod types for the validation of incoming JSON messages. This automatically generates the corresponding TypeScript types through the magic of type inference. It’s quite easy to create discriminated unions, which are just structs where one field has a fixed value.
- pyuser583 3y agoIn functional programming, a type is just a function that returns a list of possible values. It’s not a class.
- skybrian 3y agoConceptually, you can think of a type as a set of values, but no, it’s not how static type-checking is implemented. Some sets are very large (consider the set of all JSON values) and enumerating them isn’t useful.
- dragonwriter 3y ago> In functional programming, a type is just a function that returns a list of possible values Its literally not; there may be a language which does this or something close (actually, I’ve seen narrow, purpose-focussed relational languages where this is the case, and all types are enumerable), but it's definitely not generally the case in functional languages.