News for hackers. Stuff that matters.
Re:Porting 30K lines of code from Flow to TypeScript by seanwilson on Monday January 14, 2019 @05:47PM (#18907287)
Are there any people that have switched to TypeScript that still think dynamically typed languages have advantages over statically typed ones? Even for small projects with only me working on them, I find static typing saves me a ton of time and dynamic features are very rarely required or useful. The vast majority of bugs in my TypeScript programs collect around the places they interface with untyped JavaScript code.
Re:Porting 30K lines of code from Flow to TypeScript by vga805 on Monday January 14, 2019 @07:09PM (#18907813)
I bounce back and forth between TypeScript and Clojure. I've been keeping a close eye on Clojure's upcoming spec as it seems to be a nice opt-in, run-time type-checking solution. I like the flexibility and quickness with which dynamic languages let me iterate, but I want the safety. I want at least some of the benefits of both.

So yes, they (dynamically typed languages) have some advantages. Sometimes when I'm iterating quickly, especially on new projects, I don't want to fight with the compiler. BUT, I vastly prefer TypeScript to JavaScript because JS has a lot of foot-guns. I think when we get the existential operator, it will have less foot-guns, but I will probably never again write a new project for work in JS from scratch.

I really love the ideas Rich Hickey put forth in his most recent Clojure conj talk: https://www.youtube.com/watch?v=YR5WdGrpoug

The ability to spec out the shape of an object, and then in the different contexts in which you need some of that data, specify what you need, is an awesome solution to optionality (Watch the talk!). I absolutely love how much thought RH has put into the design of Clojure, and I think the final release of spec will be a close to perfect balance of dynamism and opt-in safety.

Of course, TS is a superset of JS, so adopting it little by little sorta gets me that balance I want.

Re:Porting 30K lines of code from Flow to TypeScript by wvenable on Monday January 14, 2019 @08:13PM (#18908107)
> I don't want to fight with the compiler.

The only time you're ever fighting the compiler in a modern statically typed languages with type inference is when you've made a mistake.

Re:Porting 30K lines of code from Flow to TypeScript by SmooL on Monday January 14, 2019 @08:33PM (#18908182)
Or you're trying to do something fancy that the compiler/type system doesn't support
Re:Porting 30K lines of code from Flow to TypeScript by wvenable on Monday January 14, 2019 @09:45PM (#18908503)
If you're doing something so "fancy" that the compiler/type system doesn't support it, you should probably re-think it.
Re:Porting 30K lines of code from Flow to TypeScript by zukzuk on Monday January 14, 2019 @10:56PM (#18908761)
Nah, there are still some patterns, especially around metaprogramming, that TypeScript doesn't support. One example is the inability to refer to the current class inside a static method. This makes it really awkward to write things like factory methods, which in turn forces you to either resort to copy & paste coding, or fighting with the compiler. See https://github.com/Microsoft/TypeScript/issues/5863 for example.
Re:Porting 30K lines of code from Flow to TypeScript by Nimelrian on Tuesday January 15, 2019 @06:05AM (#18910283)
Higher kinded types are currently not supported by the TS compiler and would be a highly appreciated feature.
Re:Porting 30K lines of code from Flow to TypeScript by nicoburns on Monday January 14, 2019 @10:51PM (#18908736)
Perhaps, but if that means doing it in a more cumbersome way, or with mire boilerplate, then that would be an area where dynamic languages have an advantage.

IMO, one of the geeat things about TS is that you can opt out of the type system for specific functions if you really want to.

Re:Porting 30K lines of code from Flow to TypeScript by farresito on Monday January 14, 2019 @10:53PM (#18908745)
Is it really the compiler/type system's fault, though? Sounds more like a language's design or specification flaw.
Re:Porting 30K lines of code from Flow to TypeScript by sime2009 on Tuesday January 15, 2019 @06:17AM (#18910345)
> The ability to spec out the shape of an object, and then in the different contexts in which you need some of that data, specify what you need, is an awesome solution to optionality

You realise you can do the same thing in TS with its structural type system?

Re:Porting 30K lines of code from Flow to TypeScript by emmanueloga_ on Monday January 14, 2019 @10:01PM (#18908551)
Regarding Typescript specifically, I'm a bit torn.

I used to be excited about it but after a while of using it I see it more like a tool to migrate existing JS code or to interface with existing libraries written in JS, and not like an ideal tool to write code from scratch.

The reason is, the type system is actually incredibly complicated (to accommodate to all sorts of silly and messy things you can actually do in JavaScript).

I'd prefer using a more principled language for brand new projects. Which language is that? I'm not sure yet, but dynamic vs typed doesn't seem as important to me. I think ClojureScript is a better language than TS. Kotlin/JS also looks good, for a less "alien" language that is still better typed than JS/TS. But I don't have experience working on medium/big projects with these yet.

OTOH, for browser apps, the main plus for TypeScript is that if you pick a subset, ala C++, you get an OK language that never gets too far away from what you actually get when you convert the app to JS, so easier to debug and sometimes reason about.

So there you have it... I'm torn :-)

Re:Porting 30K lines of code from Flow to TypeScript by ttty on Tuesday January 15, 2019 @06:32AM (#18910398)
> The reason is, the type system is actually incredibly complicated (to accommodate to all sorts of silly and messy things you can actually do in JavaScript).

Can you give some examples?

I have a feeling if you need that, you are writing some complicated magic code, but I might be wrong.

Re:Porting 30K lines of code from Flow to TypeScript by emmanueloga_ on Tuesday January 15, 2019 @04:16PM (#18915403)
Yeah, if you read release notes for 2.8 [1], pretty much all of the features explained are things you probably don't want to use for every-day code. I'm pretty sure a lot of those additions were done for supporting different things in existing frameworks like React and others.

The problem is, as soon as you provide those features, people will want to use them to add "type safety" to code that could otherwise be very simple. Sometimes simplicity is a better approach. This is why I say you will probably want to pick a subset of TS and leave the advanced types for interacting with legacy code or 3rd party libraries.

Also, I'll leave this link here :-) [2]

1: https://www.typescriptlang.org/docs/handbook/release-notes/t...

2: https://github.com/Microsoft/TypeScript/issues/14833

Re:Porting 30K lines of code from Flow to TypeScript by acemarke on Tuesday January 15, 2019 @10:32AM (#18911825)
Here's a couple specific examples I just saw within the last couple days.

`immer` is an immutable update library based on ES6 proxies. In addition to safely applying "mutative" code immutably, it also freezes the output to keep you from accidentally mutating it elsewhere.

An Immer maintainer just added some kind of a `DeepReadOnly` tag to its typings in a patch release, which broke things for a bunch of people:

https://github.com/mweststrate/immer/issues/289

This specifically broke our PR to convert `redux-starter-kit` to TS, with this lovely error message:

https://github.com/reduxjs/redux-starter-kit/pull/73#issueco...

I'm not saying every use of TS is that complex, but I do absolutely think that TS types can get ridiculously complex (and that people can get sucked into spending _way_ too much time trying to model dynamic JS behavior in static types).

Re:Porting 30K lines of code from Flow to TypeScript by Jach on Monday January 14, 2019 @06:43PM (#18907661)
Does TypeScript let you build a program interactively as a living thing? Example of what dynamic languages for the browser (ClojureScript) have been doing for 5 years now: https://www.youtube.com/watch?v=KZjFVdU8VLI Given that frontend work is so often an endless series of tweaks I don't know how people stand doing anything big for a long duration the classic way of edit -> compile (refresh) -> rebuild runtime state to look at what changed/play around/test -> repeat.
Re:Porting 30K lines of code from Flow to TypeScript by WorldMaker on Monday January 14, 2019 @07:10PM (#18907818)
Typescript can be indeed run directly inside the browser, though that's often not recommended, and none of the currently recommended "Hot Module Reloading" tools that I know of take that approach.

Most of the HMR tools out there support Typescript in some way directly or indirectly (Typescript plugins for webpack, etc).

`tsc --watch` is quite handy and useful in those situations where an HMR tool doesn't support Typescript or you aren't using an HMR tool at all. (For instance, on one project recently I've been just using npm package `lite-server` which implements "BrowserSync" automatic reload and `tsc --watch`, because I currently don't need a full bundler like webpack, though I expect that to change soon.)

Re:Porting 30K lines of code from Flow to TypeScript by basil-rash on Monday January 14, 2019 @07:15PM (#18907846)
To me that example doesn't look all that different from typical hot-reload provided by webpack or similar. Can you tell me what I'm missing here?
Re:Porting 30K lines of code from Flow to TypeScript by Jach on Monday January 14, 2019 @11:05PM (#18908787)
I'm not familiar with webpack but in general hot reloading isn't quite the same as dynamic redefinition, but can be close enough for many purposes. If you can set up something approaching Java's hotswap (made even better by JRebel) then perhaps the advantage is lessened.
Re:Porting 30K lines of code from Flow to TypeScript by porsager on Tuesday January 15, 2019 @02:48AM (#18909442)
You get dynamic redefinition with Wright[1][2] and chrome, and you can plug in Typescript pretty easily.

In v2 currently under way, you can use es modules which gives you a much better experience since you can reload a single file instead of an entire bundle (always sub 100ms update times).

[1] https://github.com/porsager/wright [2] http://porsager.com/wright/example.mov

Re:Porting 30K lines of code from Flow to TypeScript by austincheney on Monday January 14, 2019 @10:48PM (#18908722)
Over the past year I converted 50k loc language beautifier to Typescript. The Typescript compile time is about 5 seconds give or take half a second, which is about 99% of my total build time.

I have a tool I developed that is a combination of a web server and web sockets that automatically rebuild and refresh the browser when a code file is saved.

Re:Porting 30K lines of code from Flow to TypeScript by basil-rash on Monday January 14, 2019 @11:29PM (#18908860)
Is this a full build? Have you used `tsc --watch` at all?
Re:Porting 30K lines of code from Flow to TypeScript by austincheney on Tuesday January 15, 2019 @09:07AM (#18911166)
Honestly I wasn't even aware of --watch or I had forgotten about it. It doesn't do what I need though.

Updates to both .ts and .css files trigger the build. The build step compiles the Typescript files into JavaScript and then merges certain JavaScript files together and makes minor modifications on the fly. I prefer to do testing in the browser as its easier for me to read the code output there, so once the code is properly packaged web sockets fires a notification to the browser to refresh the page which brings in the freshly built code.

My raw TS and CSS code exists in several different files for management, but the final packaged code provides the browser 1 CSS file and 2 JavaScript files, which decreases the HTML load time.

Re:Porting 30K lines of code from Flow to TypeScript by basil-rash on Wednesday January 16, 2019 @12:06PM (#18922548)
If build time is an issue (not sure if it is or not), you can have `tsc --watch` running for the `.ts -> .js`, then also have a separate watcher for `.js` and `.css` changes that builds your final bundle.
Re:Porting 30K lines of code from Flow to TypeScript by rtpg on Monday January 14, 2019 @09:40PM (#18908480)
the problem with most static typed languages is that the type systems are just not very flexible. Typescript union types are 10x more useful than Haskell's `Either` type, and you can build extremely complex type signatures that let you build clean APIs just from that.

Static typing is nice when the tools given actually let you build nice APIs

Re:Porting 30K lines of code from Flow to TypeScript by h1d on Tuesday January 15, 2019 @06:50AM (#18910492)
Good thing about TS is that it's currently an evolving language and if you have problems with the typing system, there's a good chance it will be improved instead of other languages where it might not pick the improvement up quickly which makes you feel like the language is at a dead end.
Re:Porting 30K lines of code from Flow to TypeScript by Roboprog on Monday January 14, 2019 @06:37PM (#18907628)
Small population that would expend the effort for something expected to be of little benefit at best.

Some people can’t seem to function without static (global?) types though, and this works for them.

Re:Porting 30K lines of code from Flow to TypeScript by draw_down on Monday January 14, 2019 @06:12PM (#18907459)
I use Flow at work, and have plenty of experience with both statically-typed and dynamically-typed languages. I think it's a simple tradeoff- more certainty for more toil.

It can be a real chore convincing Flow that working code works, and this can force you to use less-readable idioms to make the typechecker happy. It also lards up your code with extra dollar signs, angle brackets, and other crud, making it less readable.

These can be acceptable costs depending on your project, particularly the size of it (in lines of code, components, number of engineers, however you care to count).