Hacker News new | ask | show | jobs
by mreiland 3895 days ago
C# generics are not the same creature as C++ templates. It isn't a matter of done correctly or incorrectly, it's apples and oranges.
1 comments

Yes I know. I've done both. Typescript's syntax appears more like C#'s generics.

My issue is that I would like types for when I get data from the server, but I don't really want to deal with casting window to "any".

I personally went through the pains of learning javascript and have come out on the other side comfortable with its dynamic types. I really like the idea of compile time error checking, but I'm not sure I would stand for adding the complexity of generics.

The change in my workflow is not very difficult. For example, when I'm using plain old javascript, I use documentation to find out how to use third party libraries and DOM manipulations. Though it works out for all of my needs, I wouldn't mind adding types. Adding types on top of that would reduce casting and spelling mistakes, but it's not like I can't open up the browser and test my code with a step through debugger. I would personally prefer opening the browser and maintaining readability in my code versus having type safety in my code and giving up readability.

I almost forgot: generic type variables are used very little outside of generic libraries. We have a 20KLOC codebase which doesn't use type variables at all and has only a single in-house written dependency [1] that declares functions with type variables.

note: This is about declaring generics, not using them. You'll still have to write Array<string> and Promise<MyType>, thats true. But its hardly any different from ArrayOfString or PromiseOfMyType in terms of readability.

If I may be so bold, I'd really encourage you to give it a spin. You just might find that your fears about readability are exaggerated.

[1]: https://github.com/doxout/promise-observer

I didn't catch this at the time, but I will take a look. Thanks.