Ask HN: Which TypeScript codebase should I study to get better?
I've been doing TypeScript professionally for the last 8 months or so and I would like to push further my knowledge of the language by reading well documented and high profile app or packages. Any suggestions ?
76 comments
[ 3.8 ms ] story [ 141 ms ] threadThe VSCode codebase is evidence TypeScript is built to scale further then Javascript.
Is it not good enough for me? Is it failing me in some awful way that I feel compelled to change it? Maybe it should get out of the IDE business if its not good enough? Maybe become a farmer, take a break from technology
General tools like an editor, a decompiler, or even an image editor can be used to modify the software itself. After all, if you're the creator of the tool itself, most likely you feel comfortable using it.
What I always wonder is how people built the first tools. How did people compile the first version of GCC? How did people build the first programming language? How did people built the first OS? I can imagine ways to solve these, but I guess the tale behind how they did it is what makes me wonder.
Generally you start with "something smaller/easier" and build your way up.
`if (x)` evaluates truthiness of `x` which in Javascript land might behave differently. I don't know how off the top of my head, but I know it's something to be cautious of in this language and its derivatives.
In other words, these are 100% equivalent:
The !! is redundant here, unless there's something relevant that isn't shown in the if statement excerpt in legohead's comment.If there was a linter rule I could turn on to require booleans in if-conditions, I'd enable it.
`!!` might seem silly, but it's a hint that you intended for coercion to happen. A similar but better example would be preferring `if (x > 0)` over `if (x)` even though non-zero is truthy.
There's an even better case for returning `!!x` from a function even though you could return `x` and let the call-site coerce it. The problem with that is that the call-site can build a dependency on the value of `x` beyond truthy vs falsey.
In a weakly typed language like Javascript, I don't think you'll ever regret being a little more explicit.
It doesn't play well with the vscode tslint extension for some reason (doesn't show up at all) so you need to run tslint manually to see the errors.. but it's better than nothing.
"Silly" is in the eye of the beholder, of course. But it is definitely redundant. Using a value in an if statement is not just a hint, but an explicit guarantee that all you are doing with the value is testing it for truthiness. !! adds no information or insight that isn't already there.
> A similar but better example would be preferring `if (x > 0)` over `if (x)` even though non-zero is truthy.
Obviously you know this, but if( x > 0 ) is not the same thing as if( x ). You can't substitute one for the other: a truthiness test on x is different from testing the numeric value of x for being greater than zero. If x is -1, the two statements will do different things.
> There's an even better case for returning `!!x` from a function even though you could return `x` and let the call-site coerce it. The problem with that is that the call-site can build a dependency on the value of `x` beyond truthy vs falsey.
That's a completely different situation. When you return a value from a function, there is no guaranteed truthiness test as there is with an if statement. You're just returning a value "as is".
So for example, if you have a function that creates some private object and it's defined to return a truthy or falsy value to tell you if the object was created, it could be tempting to say "I have a variable with a reference to the object I'm creating or null if it failed. I can just return that variable!"
As you mentioned, the caller could hold a reference to that return value, thus holding a reference to the object that isn't necessary - and could lead to a memory leak. By using "return !! myObject;" instead, you avoid passing the object itself back to the caller and just give them a true/false value instead. This is a good thing to do.
But an if statement is not the same thing. The if statement already has a guarantee that all it will do with the value is test it for truthiness and not keep any reference to it. Adding !! doesn't convey any information to the reader that isn't already there.
I like being explicit too, but I don't like adding extra verbiage to something that is already completely explicit.
"!!" is not needed in the shown case. It might be needed if you want to pass somewhere or return "boolean" value, but in the example it's only the "if" thing.
Look for code that is correct, clear, concise, testable, etc.
Think about it this way: a TypeScript compiler takes a bunch of text as input, and produces a bunch of text as output. That's not really special or weird, is it?
(The only hard part is the “bootstrapping problem” which is what happens when you want to write a compiler for language X using language X, but you can’t compile it because you don’t have a compiler yet.)
In the case of a compiler used for production, it may have lots of optimizations, most of the time, this optimizations are non-idiomatic code that's hard to understand and its purpose its not obvious.
- Scanner.Scan() https://github.com/golang/go/blob/master/src/go/scanner/scan...
- scan() https://github.com/Microsoft/TypeScript/blob/master/src/comp...
- Lexer::LexTokenInternal() https://github.com/llvm-mirror/clang/blob/master/lib/Lex/Lex...
All three examples are heavily optimized and hand-written but they’re still clear and easy to follow.
Rewrite the minimal compiler in X, and compile it using the one that was written in Y. That gives you an X-compiler written in X compiled from one written in Y.
To be sure _that one_ works, recompile your X source code with that last compiler. Now you have a compiler written in X and built with one also written in X.
Continue to add more features written in minimal-X to get X-compilers with more X features.
Ionic Framework 2+ is TS: https://github.com/ionic-team/ionic
Our new web component compiler Stencil is also TS: https://github.com/ionic-team/stencil
http://typeorm.io/
It's also one of the larger enterprise grade projects that i've seen done natively in TS. It pretty much uses all the bells and whistles that the language provides. Some decent patterns, and an active developer community.
There is this issue talking about production readiness with more feedback:
https://github.com/typeorm/typeorm/issues/591
You should know about quirks like this one:
http://typeorm.io/#/relations-faq/avoid-relation-property-in...
I've been spending more and more time reading the code and tracking the open issues - getting comfortable with phasing it into a production system
1. InversifyJS - DI in Typescript / JS
https://github.com/inversify/InversifyJS
2. TypeORM - as it says, might be the best ORM for nodejs projects:
https://github.com/typeorm/typeorm
3. Searchkit - ES web frontend - react + typescript
https://github.com/searchkit/searchkit
Also, thanks to the Inversify contributors for continuing to maintain and improve express-utils!
the Typescript ecosystem has really come a long way
Do you know if there are any plans for allowing the http context (req, namely) to be injected into services? Similar to the IHttpContextAccessor in dotnet core?
I have some use cases for using pieces of the http context (headers) to instantiate some transient services. I started putting together my own pieces to see what I could get away with. Looks like it works okay, but I had to create a new container per request, using the `Container.parent` to link them.
Looks like a class to me: https://github.com/Microsoft/TypeScript/blob/master/src/comp...
// Keep the class inside a function so it doesn't get compiled if it's not used.
I didn't know it was possible.
Is the reasoning behind this documented anywhere?
I could only find instructions the coding guideline indicating as much (here: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelin...) but couldn't find why. I'd love to know why, may better inform my usage of typescript.
you have all the answers in front of you do you need them to read the code for you? Come on man. A little effort please. Its not like you have been neglected with info.
[1] - https://github.com/Microsoft/TypeScript/blob/master/src/comp...
It's also strange to me why they inline their interfaces, rather than organizing them in a common folder / module.
It has fairly good docs: http://doc.babylonjs.com/ as well as an active forum for developers and users: http://www.html5gamedevs.com/forum/16-babylonjs/
Other options mentioned are also good - the Typescript compiler (https://github.com/Microsoft/TypeScript) is good to get started with especially if you read this Gitbook: https://basarat.gitbooks.io/typescript/content/docs/compiler...
You could also search popular typescript github projects that are more closely related to what you work on, but the Typescript compiler and Babylonjs 3d engine are both well written projects with fairly good documentation in my opinion.
https://github.com/ihsw/toxiproxy-node-client
Reading the NativeScript code gave me a really good impression of TypeScript, although I'm just learning it and haven't written much yet myself.
https://www.nativescript.org/using-typescript-with-nativescr...
I have a huge blob of old JavaScript cellular automata code I want to rewrite in TypeScript, but first I want to read some good TypeScript code before starting so I don't mess it up.
It has a bunch of old "frameworky" meta-programming code and data structures that I've painted myself into a corner with (see the "type definitions" comments), which I'd like to throw away and re-implement from scratch, because TypeScript is much better suited for that kind of stuff. And then there's a bunch of brute force bit bashing and number crunching code that I hope will just run without any modification and only a little repackaging, which is another nice benefit of TypeScript.
https://github.com/SimHacker/CAM6/blob/master/javascript/CAM...
That might sound dismissive of TS but it’s not, I’m just trying to get a sense on what you’re trying to learn from TS.
https://github.com/PolymerLabs/lit-html
https://github.com/palantir/blueprint/tree/master/packages/c...
1. How has been your experience with Blueprint JS?
2. Is it possible to take a look at your work?
3. Would you recommend using it? If yes, what types of apps?
It is pretty damn thorough tho and the css is relatively minimal if you need to custom skin the components.
Yes I would recommend it highly. Just look through the components they have mostly everything covered.
https://github.com/ReactiveX/rxjs
https://github.com/glimmerjs/glimmer.js