Readit News logoReadit News
montroser · 4 years ago
I would welcome this making its way into ECMAScript not because I like TypeScript, but because it's a drag to see TypeScript enthusiasts litter the JavaScript ecosystem with all of the piles of bolt-on TypeScript baggage.

Forking projects just to add type definitions, or else mega repos like DefinitelyTyped madness; bringing all of the terrible complexity that we love to hate in Webpack/ESBuild/Rollup/etc back over to Node.js with server-side tsc; drive-by PRs to all my open source projects from inspired people trying to add types and me having to disappoint...

I would gladly do without all of that. To me, I see the TypeScript tradeoff as coming with only marginal upside, in return for a large downside as described above. But, others passionately disagree and see a huge upside, and that's cool.

In the end, I want us all to be happy, and I think this proposal would have hope getting us there. For this to be really spec'd and standardized would be a green light even for skeptics to invest in deeply learning and internalizing. To get to avoid yet another build-time compilation step would be bliss. To have us all back in on the same team would be glorious.

keawade · 4 years ago
I disagree that TypeScript comes with only a marginal upside. In my experience TypeScript projects are much more approachable for contributors.

For example, I once was debugging an issue in a JS framework and while I knew what was wrong and where I could fix it I didn’t have any idea what existing tools I had available in that context to fix it. Having type definitions would have made that work much simpler by increasing the accessibility of the code base.

It’s experiences like this that have convinced me that if I’m writing code that any one will possibly read or need to modify later (including if it is just me) then I should be writing TypeScript instead of JavaScript. Or, more generally, that it should be typed.

datavirtue · 4 years ago
Raw JavaScript in any production app is an abomination. Constant issues because one of the variables had a errant character. Countless bugs because of the same or it not being defined. On top of all that how in the hell am I supposed to know what a parameter or return value is supposed to look like? I have other things to worry about. I think these type annotations would make everyone happy. The IDE can navigate and inform but the objects will still need to be defined somewhere. I assume we can define a custom type with these annotations?
shadowgovt · 4 years ago
You and OP are both right, because it really depends on what you're doing.

At the scale I operate, it would be corporate suicide to try and write this code in straight JavaScript without type support. But for smaller teams, more focused teams, and teams that use a heavy testing discipline or enforced naming conventions that supplement the lack of static typing, it's not a problem.

lostcolony · 4 years ago
Your basic disagreement here feels like it would be obviated by the parents "I hope this makes its way into the ECMAscript spec"? I.e., you would get typing without any of the tooling Typescript requires; the parent's point was JS + typing would be a pure benefit compared to Typescript, and you've said nothing to disagree with that.

So while you technically are disagreeing with the parent, it's on the smallest of points, and completely disregarding the main one the parent offered (which, fair enough, but wanted to point that out)

pwdisswordfish9 · 4 years ago
What does "while I knew what was wrong and where I could fix it I didn’t have any idea what existing tools I had available in that context to fix it" mean?
NoWizards · 4 years ago
I couldn't disagree more with you... All i see when working with different levels of experienced contributors is param:any any[]. Even more when you want to interact directly with DOM instead of using an opinionated framework.
Waterluvian · 4 years ago
I used to feel the same way about TypeScript until I really gave it a try one season.

I feel like I was crippling myself for years, being an inferior developer wasting so much time with the things that TS guards against, documents, and autocompletes for.

I support any formalization of TypeScript that further tidies up the "add-on" nature of it. But to be honest, it's really quite fine already.

edgyquant · 4 years ago
I work at a place that is full stack typescript with a legacy app that is Js/react (the kind of stuff I was writing in 2017-2018.) After using typescript and going back to fix issues on the legacy codebase I now understand why the industry adopted Ts
spion · 4 years ago
This isn't going to fix those issues.

Those issues aren't due to tsc. They're due to an ecosystem which refuses to recognize the realities of compilation and add first class support for it. Its also due to an ecosystem willing to break compatibility (and not just backward compatibility but compatibility to other tools) at the drop of a hat.

For example, the main reason I've seen why people began using bundlers for node recently is the fact that a lot of modules transitioned to pure ES6 without CommonJS support. In most projects, this means you have to use a bundler to be able to import those modules, or switch your entire codebase to ES6 (which is non-trivial due to changes in import path semantics).

If it weren't for the above, you could just run `tsc` to get a `.js` file in `dist` for every `.ts.` file in `src` and run node on those.

afavour · 4 years ago
> They're due to an ecosystem which refuses to recoginze the realities of compilation and add first class support for it.

IMO this is the correct path. Compilation should result in WebAssembly and it seems like the tooling is finally getting there.

spion · 4 years ago
Of course, using a bundler means you'll face several other unpleasant realities. Like

  - what about tests, how do they run
  - source maps support in node being 3rd party and unbearably slow for any medium sized project 
    - edit: this has been fixed somewhat recently https://nodejs.medium.com/source-maps-in-node-js-482872b56116 
    - only the 3rd party bit, not the slowness bit https://github.com/nodejs/node/issues/41541
    - also applicable when not bundling, albiet fewer reprcutions to turning off source maps
  - poor native (C++) modules support in bundlers 
  - modules with really dynamic `require` statements being supported at varying levels by varying bundlers

dlojudice · 4 years ago
> To me, I see the TypeScript tradeoff as coming with only marginal upside, in return for a large downside as described above.

I've coded using static and dynamic types. C#, Java, Ruby, JS, etc. 20 something years doing it, mainly for business applications (opposed to system or embedded). My personal opinion is that types bring an elusive value of control and maintainability. Confronted with legacy / spaghetti code, developers with less experience will bring lint, code style, some layers and ... types, as a way to solve the problem of long term maintainability.

But unfortunately it does not bring that much value. Actually it brings costs to readability and unnecessary abstraction (interfaces, generics, etc) aka "fighting the compiler".

A better test suite can have exponential more value on maintainability than types.

> Static Typing was the most requested language feature in the State of JS survey in both 2020 and 2021. [1]

So when I see static types been the most requested feature I wonder how many of them know how to create a good test suit or a good architecture. If its behind this feature request is a different pain to be solved.

[1] https://github.com/giltayar/proposal-types-as-comments/#comm...

azangru · 4 years ago
> A better test suite can have exponential more value on maintainability than types.

A lot of ink has been spilled over this question. The conclusion is: no, tests do not and cannot replace types. Types, in themselves, provide a layer of tests that nobody, typically, would think of writing (what if a function is called with fewer arguments? what if it receives a different type of argument from what it was expecting?) and eliminate a whole class of superfluous tests that are checking for things that are better checked with types.

The self-documenting property of types is remarkable. Jsdocs do not hold a candle to a typed codebase.

Plus, refactoring. Even with tests I would be scared of large refactorings if I didn't have a type checker to also hold my hand. Of course, maybe I just don't know how to write code properly.

eyelidlessness · 4 years ago
> Confronted with legacy / spaghetti code, developers with less experience will bring lint, code style, some layers and ... types, as a way to solve the problem of long term maintainability.

> But unfortunately it does not bring that much value.

If you mean adding these things post-hoc, yes the value is limited. If you start with types, and you embrace them as a design and testing tool, they can literally change the kind of code you write in the first place.

People talk about how convoluted TS types are, but the reality is that’s because they’re describing convoluted JS. TS-first code tends to be a great deal more straightforward, because you define the interface before implementing it.

In other words, if you start out with static types and think about them first, you’ve likely already got a good architecture and test suite.

b6dybuyv · 4 years ago
I guess there are differences between how static typing is implemented.

I've been using Elm for the last 2 years and it's not that I don't fight the compiler, but I constantly use it to guide me while refactoring. Elm code is extremely readable and types (including Maybe, Result, etc.) just give so much confidence and control to the programmer. While working with an Elm code base, refactoring is a constant, joyful, safe act.

It is true that in many cases good tests help. However often when I work with Python (using type hints), I need to write a unit test just to ensure a contract that I could explicity state using types in Elm. It doesn't feel right.

zarzavat · 4 years ago
Unit tests aren’t a replacement for static types. Tests aren’t machine readable so you lose all those nice IDE refactoring features and have to manually keep them up to date. Also running a test suite takes time, it takes a lot longer to run a test suite than it does to get live typecheck, which means you cannot use tests while you are editing. And you probably don’t want to clog up your test suite with endless “x is a Y” tests.

In general dynamic types incentivize the status quo and promote codebase ossification, and somewhat ironically make rapid prototyping harder.

Deleted Comment

ht85 · 4 years ago
Do you write a lot of library code? I'm genuinely wondering in what kind of environment you would get "marginal upside" from having a good type system.
riskable · 4 years ago
I've been coding JavaScript for many, many years. I've never encountered a situation where the lack of typing bit me in the ass. Not even once!

Having said that, it might be because when I'm writing JavaScript it's just me. Normally I'm the sole developer. So of course I know my own stuff! I'm not going to experience an error passing the wrong type to a function because I literally wrote all the functions myself. I even have enormous libraries of JS code that I use in my personal and work projects where I wrote everything.

This developer is probably in the same sort of situation. Type errors are the kind of thing you run into when you're working with large codebases that were written by lots of different people. It's not the type of thing you experience with your own code.

I've also written loads of Python and never experienced an error where the wrong type gets passed to a function (even working in a team). For years I couldn't even fathom how static typing would be a benefit other than maybe it can speed things up by giving a compiler/interpreter the ability to optimize things. Then at work--after nearly 20 years of Python--we finally had an issue where the wrong type was passed (float was inadvertently rounded because the function was made for integers and it wasn't obvious). It was caught before the code was sent to production (and it wouldn't have been a huge deal anyway) and I finally had a real-world situation where type annotations would've prevented an error.

It's just not something that happens very often. It's the type of thing that creeps up on you for sure but it's not a common, every day occurrence. Type-related bugs are really super obscure. They're far, far rarer than type-obsessed developers make them out to be.

afavour · 4 years ago
For what it’s worth I agree with you: after spending years battling with Babel and all that nonsense it’s been amazing to be able to just write ESM JS and have it run both on Node and in the browser.

The real lightbulb moment for me was using JSDoc annotations with TypeScript types in JS files. It definitely isn’t as graceful as JS itself but it’s not that big of a deal, plus it encourages you to add comments. And any TypeScript editor will parse the code as if it’s TS, provide autocomplete, type checking etc etc as required.

kingdomcome50 · 4 years ago
Found the guy who hasn't used Typescript beyond tinkering!

I kid... mostly. But in all honesty I have found very few instances of developers who have worked significantly with both languages and have come to the conclusion that TS has a marginal upside (though maybe they will come out in defense here!).

claytongulick · 4 years ago
<raises hand>

I've worked extensively with both.

I find that ts normally brings a downside, actually. In one project I maintain, a solid 30% of the code is nothing but ts stuff.

It makes maintaining the code base excruciating.

Small changes between versions of ts or .d.ts files have caused hundreds of errors. A concrete example of this is the change in mongo driver that forced "new ObjectId()" instead of "ObjectId()" though functionally there was no difference.

I see massive bizarre and difficult to grok type declarations that grow in excruciatingly byzantine complexity just to satisfy the compiler for non-trivial flexible function definitions.

I see painful compilation times, a beefy laptop's CPU getting pegged and the fan whining while trying to do even minor updates.

Try to do a build while on a video conference? I see two to three minute build times.

I see a series of nonsense stories in sprints that are nothing more than activity over achievement. They don't drive product functionality, they are a bunch of make-work to satisfy the developer asthetic and the compiler.

The nightmare of "compiled" NodeJS production support is a dystopian hellscape that I'm forced to endure every day because of NestJS and typescript. And nothing in that has done anything to increase code quality or reduce bugs from the crappy offshore team that wrote it.

Clearly, I'm not a fan.

That being said, I think type comments in jsdoc are a honking good idea.

I use inline /* @type */ declarations and .d.ts files liberally.

If we could add to jsdoc the ability to document types and purpose of arbitrary js objects inline, at definition time, instead of an external .d.ts file, I'd be a happy guy.

An example of this would be sequelize schema defs, or mongoose schema defs, where I can have all my jsdoc code comments and type definitions in the same place as the code.

All that being said, I like the idea of this proposal. Especially if it could solve the schema definition issue above.

I think optional typing is a good idea to bake directly into the language. I thought AS3 did a great job of this.

I even think it would be great to have an equivalent of "strict" that would enforce typing at the module level to enable straightforward AOT / wasm interop (I know it doesn't solve the GC issue).

My beef is mostly with the extra compilation step and needless complexity of ts.

If we had simple (no generics) opt-in typing that was natively supported by the engine, I think it would enhance the language overall, like type hints have done for python.

IshKebab · 4 years ago
You're wrong - the benefits of Typescript are huge, and the size of the hoops people are willing to jump through to get those benefits should indicate how big they are.

I do agree it would be nice to simplify some of the tooling though.

simion314 · 4 years ago
Just finished debugging one of those ugly issues where it works fine for the developer but for some other person things are broken. it was a piece of code that assigned an Number to a property that should have been an object(the dev confused the property name). Because the project is complex and there is a lot of network requests and stuff running in different order depending on the user internet the bug did not happened to everybody. You could work aroudn this using setters and doing runtime checks but I have a feeling people that don't like types also don't like getter and setter.
markstos · 4 years ago
Are you saying that if there had been type checking, this issue would have been much easier to detect and prevent?
fifticon · 4 years ago
I abandoned my attempts to make larger projects in pure javascript. I happily write larger projects in typescript. Typescript makes javascript feasible, usable for me.
seniorsassycat · 4 years ago
Would you use type comments in your projects? Would you like pull requests adding them, or fixing them? What if the typescript checker and another checker have different semantics and you have fans fighting between `number` and `u8`?
yboris · 4 years ago
You should try Vite - https://vitejs.dev/ - start a TypeScript project in under a minute - no annoying Webpack configuration needed.
cphoover · 4 years ago
Stop trying to make JavaScript into something it's not...

If you want a type system Typescript works great, and it's very easy to set up. You even get debugging to the original uncompiled source with sourcemaps.

If you want a strongly-typed static language, compile to a wasm target... That's what it is for.

Stop trying to change JavaScript into a language it is not.

JMHO

halfmatthalfcat · 4 years ago
I mean, with all due respect, who are you to decide what Javascript is and is not? There's obviously a huge demand for type safety in Javascript, hence the existence and adoption of Typescript.
cphoover · 4 years ago
I don't know but I've been using JavaScript for a while now. It seems like a lot of the people that want JavaScript to look similar to other languages are coming from different programming fields, and want JavaScript to look more like their language of choice.

Most of the changes to JS in the last 20 years, have been serious improvements, but they did not change the fundamental characteristics of the language.

Believe it or not there are a lot of people that like JavaScript!

kyberias · 4 years ago
But didn't he merely express his opinion about this?
Turing_Machine · 4 years ago
Compare the lines of code (on Github, say) written in straight Javascript and the lines of code written in Typescript.

I do not think the demand is quite as huge as you think it is.

dangerbird2 · 4 years ago
Did you read the article? All the proposal does is allow the javascript engine to accept typescript-style syntax, while erasing all the annotations at runtime. From the browser's point of view, it would essentially treat the type annotations and type declarations as comments. This combined with native Ecmascript modules means you could theoretically develop a typescript application with no bundler or other build tools. You'd be able able to use typescript autocomplete and linting in your editor, and just serve the files to your browser with a static http server. Some newer bundlers like Vite and esbuild greatly reduce the amount of configuration required to set up a typescript project, but being about to develop a project with nothing but an editor and a browser would be a huge win for small projects.

Since the proposal doesn't care about the semantics of the type annotations, it doesn't even necessitate typescript. It would work just as well with Flow typing or even a completely new type checker.

jiyinyiyong · 4 years ago
> From the browser's point of view, it would essentially treat the type annotations and type declarations as comments.

V8 still has to updater its parser. Other tools that handle JavaScript parsing need to do that as well.

> You'd be able able to use typescript autocomplete and linting in your editor, and just serve the files to your browser with a static http server.

Nope, it's not using TypeScript directly, but just another language very similar to TypeScript.

balefrost · 4 years ago
Out of curiosity, where do you stand on ES6 classes?
cphoover · 4 years ago
Certainly cleaner than using the old prototypal inheritance syntax.

    // Dog Class
    function Dog(_name){
       // call the parent constructor
       Animal.apply(this,arguments);
    }

    // extends the Animal prototype chain
    Dog.prototype = new Animal();
Although the new class syntax may obfuscate from what's actually happening in an effort to look more like traditional class-based languages like Java.

However I'm the type of person that will only use React function components in my codebase. I don't think the object-oriented style has added much too my programming, except the temptation to reach for an unnecessary abstraction.

riskable · 4 years ago
The only constant is change! The "don't make JavaScript into something it's not" ship sailed decades ago when AJAX was thrust upon the world.

Besides, adding even more complexity to the JS ecosystem at this point is like pouring a cup of water into the sea.

nine_k · 4 years ago
Do you remember Javascript from 1996? (I do.) Do yo think that all the improvements of ES5 (reflection, strict mode, JSON) changed it into what it was not? Did ES6 (classes, imports) change JS into something it was not, or should not be? Did ES2016 (async) do that?
nobleach · 4 years ago
I get that this is a humble opinion. And I respect that. But for a language that draws so much ire from the programming community, and yet demonstrates such an ability to change and morph over the years. Are you saying you do NOT WANT it to get better? (and I realize "better" is subjective).

The same is true of PHP. People hate it. People complain. PHP takes the criticism and makes updates. People complain more.

cphoover · 4 years ago
Of course I want it to get better. I just don't necessarily agree that a type-system = better, particularly in the way in which it's been suggested. Especially when you aren't talking about changing the internals regarding how JS is executed or how data is represented, you are only talking about adding "comments".

JavaScript has evolved as an untyped language, it has type-coercion and a single way to represent (most) objects in memory. So regardless of what kind of syntax you add on top... it will still be a untyped dynamic language internally. Are we talking about changing this truth? Or just adding additional syntax for developers?

CraigJPerry · 4 years ago
It does feel like we have a problem saying no to language features, and it’s not just in the JS world.
franciscop · 4 years ago
Definitely, ES6 was great and it seems we've ramped up to add as many features as possible, which has produced a lot of churn and unused/hated features. A committee was formed to add new features when needed, and now they... just keep adding and adding even when not needed.
datavirtue · 4 years ago
That ship already sailed. If we were just twiddling the DOM I wouldn't even be reading this thread right now but people are writing production back-end code for financial services companies in JS. Bring on the type system.

Deleted Comment

Salgat · 4 years ago
This argument could be used for literally any improvement to JavaScript.
kall · 4 years ago
I think this proposal is trying to keep in that spirit while making everyone happy, by only adding what is effectively another comment syntax to JS, instead of defining a type system for it.
monocularvision · 4 years ago
I am mildly confused by your objection. It appears the proposal is to allow type annotations to appear as essentially no-op instructions. It isn’t adding a type system to JS.
jiyinyiyong · 4 years ago
partially agreed, I would love to see tools and supports of WASM being put on a higher priority and people improves dev experiences there. JavaScript syntax is already coupled with too many factors being a scripting platform or a compilation target.
wolfadex · 4 years ago
Everyone keeps saying variations of "It would become possible to program (e.g.) TypeScript without compiling the source code." but that's not true. This proposal is only a subset of TypeScript, meaning that all of the existing TS infrastructure would remain and still be used. All this does is further bloat the ECAMScript for no gain.
eyelidlessness · 4 years ago
I agree that there are problems with limiting this to a subset. But for the most part it’s a very good subset that mostly only removes features which would be rejected if proposed today, because they have runtime semantics. This would effectively deprecate those features in the real world. I’m in the minority who likes enums, but I can live without them. The real, big gap here is JSX. But that rightly should be a separate proposal. I just want them to consider it a prerequisite, to avoid fracturing that aspect of TS.
cphoover · 4 years ago
I agree. The TC39 committee process has had a lot of advantages in modernizing the language, but in the last 3-5 years we've seen the pace of merging new language features grow in an unprecedented fashion. There are advantages to evolving the language but there is also a risk that the syntax becomes bloated if there is not enough deliberation within the standards process.
endemic · 4 years ago
I like the idea of TypeScript, and am learning it, but am disappointed that we're still using build systems for JavaScript. I thought modules, HTTP2, and evergreen browsers would allow us to write just plain JS (like the olden times!), and not have to worry about bundling, but I guess not.
Vinnl · 4 years ago
You'll still want to compile (or more specifically, type-check and minimise), but you'll probably be able to run your TS code without compiling, if you stick to that subset - but that'll likely be pretty easy. I'm imagining lint rules like the ones we're using to restrict ourselves to const and let instead of var, and things we'll quickly internalise like avoiding enums, and things we're no longer using anyway, like namespaces. Yes, it's a subset, but includes enough that you don't need the infra to run it.
ARandumGuy · 4 years ago
This was my first thought as well, but I do think it could still be useful, even if not all TypeScript can be run as pure JavaScript. There's a lot of use cases where some type hinting would be useful, but the full feature set of TypeScript is not necessary. Long term, I could see a world where these features effectively replace TypeScript, which could help simplify dev pipelines.
wolfadex · 4 years ago
How would these features replace TypeScript? TypeScript is more than just a syntax as it also requires a compiler, config, IDE tooling, and typically additional plugins to make it compatible with testing tools and bundlers.
015a · 4 years ago
To be frank, I hate to be the typical HN luddite, but this proposal [1] feels like it will add too much parsing complexity to runtime JS.

The counter-argument to that is: everyone minifies code anyway, but this feels like a very poor argument. "Adding cruft is OK because most people have complicated build chains to get rid of it" is very different from "we've opted into a build chain (tsc) and now have access to adding cruft"; there's a default situation here, which is the many people who write JS (FE or BE) and aren't minifying it.

Additionally, their pipe dream of allowing TS to become standard JS "if they stick within a certain reasonably large subset of the language" (an actual quote from the TC) is... wild. Wild. No thoughts, just vibes. Typescript is an extremely mature, fast-moving, complex project. Their list of TS features not supported under this TC is only three items long (with enums and namespaces among the list, lol), but I guarantee there are TONS of very complex type-level assertions typescript is capable of which JS is years, maybe decades, from reaching considering the glacial speed ECMA moves at.

Typescript will always be a superset, which they admit. The problem is: I hold extreme doubt that JS/ECMA is even capable of communicating what that differential is to average users. Will JS support control flow analysis for dependent parameters, aliased conditions, and discriminants? Const assertions? Middle rest elements in tuples?

Typescript adds all these assertions, and more, every couple months, because it turns out that type systems are something of a pandora's box. Few language-level type systems remain at the simplicity of, say, Go; most become TS, or Rust, or Java. That's not a bad thing, but not everything should be that.

If this gets traction and built, JS will in-effect, though not in-requirement, turn from an interpreted language to a compiled language, whose compiler isn't even maintained by the core development teams. That's a major, major shift; and I'm not sure the proposers have even considered the ramifications of it.

[1] https://github.com/giltayar/proposal-types-as-comments/

justshowpost · 4 years ago
Seems like you entirely missed the “as comments” part. In the current proposal at least, the type annotations are just comments that can not change runtime semantics.
015a · 4 years ago
No. They still need to be interpreted at runtime in unminified code, in order to determine that they are comments. This carries a cost; performance, sure, but more importantly it locks in a SWEEPING complex syntax change for decades which limits future language changes and significantly complicates the parser.

The fact that they by design have no impact on the runtime isn't really even a positive. That could be a large tactile benefit of this RFC; that suddenly JS is capable of communicating to its runtime what types different primitives are, such that the runtime can optimize their storage and usage. But, by design, they don't want this. Maybe that could change in the future, but I suspect it won't because the proposers have taken the stance that everyone minifies anyway so the syntax doesn't matter anymore.

mbf · 4 years ago
Erasable types are good enough with source mapping. TypeScript workflows are fine. ECMAScript really doesn't need to absorb a TypeScript syntax. It's not necessary to keep bloating the runtime with a feature only useful to the developer.
eminence32 · 4 years ago
If the runtime is going to be fully ignoring these type annotations, what type of "bloat" are you worried about?
windows2020 · 4 years ago
New syntax and concepts.
xigoi · 4 years ago
Larger file sizes.
cphoover · 4 years ago
larger downloads, larger syntax
azangru · 4 years ago
Typescript, by the virtue of not being javascript, has evolved pretty rapidly, and has come pretty far. Would the addition of types to javascript and typescript's conformity to those type annotations slow down typescript's development? There's still some hope, at least on typescript's roadmap[0], for nominal types in addition to structural types. Will the standardisation of type annotations impose further constraints on typescript?

[0] - https://github.com/microsoft/TypeScript/wiki/Roadmap#future

Vinnl · 4 years ago
It's not the addition of types to JS, it's telling interpreters to ignore parts of JS source files that can then be used to insert type annotations into much like comments, e.g. for nominal types.
ravenstine · 4 years ago
I'd be more optimistic about official type annotations for JavaScript if said typing was less like the particularly unique syntax of TypeScript and more like that which already exists in C-family languages. For instance, in Typescript, we can do this:

---

const user: User = {

  ...
};

---

Why couldn't this be more like C rather than this weird conflation with the existing colon token?

---

const User user = {

  ...
};

---

That might seem not as pleasing to those familiar with Typescript, but it's much closer to other existing syntaxes (meaning less of a learning curve coming from Java or C).

Same with function definitions:

---

function foo (bar: string, baz: boolean): Qux {

  ...
}

---

Versus:

---

Qux function foo (string bar, bool baz) {

  ...
}

---

I find the latter much more familiar than Typescript and less confusing. It's also slightly less verbose.

S1ngleM4lt · 4 years ago
That seems to be mostly just a matter of taste, but is definitely off putting when coming from other languages. Interestingly other recent languages like rust, zig, and go follow a similar convention to type script. Go has a brief explanation of why they went with this approach [0] so I’d be curious if other langs have documented their decisions.

[0] https://go.dev/doc/faq#declarations_backwards

Etheryte · 4 years ago
My completely subjective guess is that it might be to avoid ambiguity around reserved keywords. For example, the `void` keyword [0]:

  void function () { return 42; }(); // returns undefined
It's not something you would usually use in your code, but many Javascript minimizers make heavy use of it, and Typescript must know how to handle any valid Javascript without ambiguity.

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

seniorsassycat · 4 years ago
: type syntax will be more familiar to rust, kotlin, and typescript devs. Why is c relevant?

The name is usually more important than the type so I like that typescript puts it first.

jestar_jokin · 4 years ago
As you say, it's just a matter of familiarity. Sounds like you just need exposure to more programming languages/paradigms, as it's a pretty common convention. I believe it comes from the ML family. OCaml[0] springs to mind. As others have mentioned, Rust[1], Kotlin[2], Haxe[3], Nim[4], Swift[5], Elm[6], Dhall[7], and Scala[8] all use this convention.

The current syntax seems better for type inference and gradual typing.

[0] https://learnxinyminutes.com/docs/ocaml/

[1] https://learnxinyminutes.com/docs/rust/

[2] https://learnxinyminutes.com/docs/kotlin/

[3] https://learnxinyminutes.com/docs/haxe/

[4] https://learnxinyminutes.com/docs/nim/

[5] https://learnxinyminutes.com/docs/swift/

[6] https://learnxinyminutes.com/docs/elm/

[7] https://learnxinyminutes.com/docs/dhall/

[8] https://learnxinyminutes.com/docs/scala/

orif · 4 years ago
Actually, I would prefer a C/C++ dialect in TypeScript’s type notation. Parsing time/complexity would reduce drastically, and would allow type inference.

Deleted Comment

danielvaughn · 4 years ago
Yes exactly. There must be some good reason why TS chose the syntax they did, because a lot of it feels very old-school-java.
stasm · 4 years ago
Type inference is one good reason. Similar syntax exists in a few modern languages, too: e.g. Rust and Swift. In languages with type inference it can be convenient that type annotations are in the postfix position relative to variable names:

    let user = new User();
    let user: User = new User();

kderbyma · 4 years ago
then we would have another type system....no just stick with typescript or none at all...
ravenstine · 4 years ago
Well... yeah, I don't actually advocate for another type system, but more that if we have any type system at all for JavaScript then asking why we're going with one that overloads the colon token when it's already been sufficiently overloaded in the first place. Typescript already is what it is, and I think we should just stick with it, but if we are going to get yet another type annotation system then perhaps it might as well not be as novel as Typescript if there's a benefit.
synergy20 · 4 years ago
Even better, make sure browsers typescript ready or fully compatible over time. In my opinion Typescript and Javascript should merge into one and gradually shift towards Typescript. I used to feel TS is just extra effort unneeded, after using it for a while, I found it so great I no longer want to code in JS, not at all.
madeofpalk · 4 years ago
The MS blog post, and actual proposal covers this fairly well imho:

Typescript's type checking is slow and doesn't need to happen at runtime. Additionally, it moves failures to users rather than developers and makes it incredibly difficult to change type semantics later on.

https://devblogs.microsoft.com/typescript/a-proposal-for-typ...

https://github.com/giltayar/proposal-types-as-comments/

chess_buster · 4 years ago
Me, too.