Hacker News new | past | comments | ask | show | jobs | submit login

Typescript doesn’t actually do runtime checking though does it?

So if I wrote a library in ts and someone called the built js directly it wouldn’t have any type checking would it?




No it doesn't.

This may not be a popular opinion for those coming from C or Java, but runtime type checks in JavaScript are usually an anti-pattern. If my function defines a contract that the caller violates by passing in null or undefined, then my function should raise an exception when it tries to access `undefined.xyz` or whatever. Otherwise, embrace the duck! If the function can work with what it was given, it should instead of trying to enforce some Java-esque type system at runtime using typeof or instanceof.

But be pragmatic. If there is some critical area of your app that absolutely must never ever throw, typecheck away (although this is JS after all, so it still may throw if it damn well pleases).


> If the function can work with what it was given, it should

How do you know that it "can work" based purely on the name of the function?

> But be pragmatic

I am. I consider javascript a last-resort option, only really useful for adding minor enhancements client side. Beyond that, I don't use it.


Documentation and/or a type checker. Jsdocs are both documentation for programmers and can be enforced using tooling like the closure compiler or typescript

JavaScript is no different from most other dynamic languages this way


So, 'hope'.

This is exactly why I can't take javascript seriously for anything besides basic UI enhancements to a web page.


I’m sorry that I commented on this article. Knew it was a trap. I’ve been writing functional-style for a long time and just wanted to share a bit of what I’ve learned.

Use whatever tools work for you, but also don’t disparage or dismiss other tools because they have different strengths and weaknesses than what you know or appreciate. There is no need for proselytizing; these are just tools after all!

I like JavaScript and TypeScript because they have let me quickly build many great things and share them with people. I also subjectivity find writing in them to be enjoyable. I may always turn to a different language if it is more productive and enjoyable than js/ts for the task at hand.


When you are doing interop between languages a lot of things can go wrong. Rust is pretty much doing compile time checks too.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: