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

TS is what everyone is coding nowadays, and yes it definitely "fixed" the problems with JS.

However the world would be a better place if there was just Java, and neither of it's two offspring were ever born (JS and TS).




Yet, it compiles to JavaScript and it allows many of the things that make JavaScript unsafe.

JavaScript is highly mutable and under the right circumstances you can trick JavaScript applications to modify themselves. i.e.: Prototype pollution.


You can write 100% perfectly type-safe code in TypeScript. You'd have to accidentally do a 'var' instead of a 'let' variable, or forget to add a type to a parameter, but if you try you can get pretty much 100% type-checking on every letter of every variable or object.


Run this, please.

    // 100% type-safe function :-)
    function f(a: Date): number {
        return a.getDate();
    }

    // Input to 100% type-safe function
    const p = new Proxy(new Date(), {
        get: function(target, prop, receiver) {
            if (prop !== 'getDate') {
                return Reflect.get(target, prop, receiver);
            }

            return function() {
                console.log('Not typesafe :(');
            };
        }
    });

    f(p);
Output: "Not typesafe :("

What happened? all type-safety was circumvented. The f function that should return a number did not return a number. In fact, it didn't return anything.

The TypeScript compiler didn't return any errors either. This is perfectly valid TypeScript.

Why? because TypeScript is based on JavaScript. It is not 100% typesafe and it will never be.

Is TypeScript an improvement? yes. Does it help a lot of people? yes. Is it 100% typesafe? no.

Did TypeScript "fix" JavaScript? Mostly, but no. I hope the mods unflag my original comment.


I had already given two examples of how you can create non-type-safe code in TypeScript. I mentioned 1) the 'var/let' way and 2) The "Not including types on parameters" way.

You then gave an actual example of #2. lol.


That is not what is happening here. TypeScript correctly infers that type of the variable is a Proxy for a Date object. If you do not supply a Date object as first argument, the code won't compile.

The part that breaks things is that when f invokes a.getDate(), the Proxy get trap changes will return another function instead of Date#getDate at runtime, and that wasn't validated at compile time.


Proxy is part of JS. When did I ever say ANYTHING in JS is typesafe? Nothing in JS is typesafe.

However this is also simultaneously true: You can code in TypeScript and achieve 100% type safety by putting types on everything. If I claim all cubes can be painted red, you can't disprove that by proving green cubes exist.


And what does TypeScript compile to? JavaScript.

Does TypeScript forbid using JavaScript features? no.

Can TypeScript realistically verify that your program is type-safe? no.

Is TypeScript 100% type-safe? no. They wanted fast adoption, therefore they allowed JavaScript and untyped variables.


You can go into my 250,000 loc project and randomly pick any variable name, method name, parameter name, etc., and put a typo in it or wrong type, and the compiler will catch it.

I have 100% type safety, and it's all done by TypeScript and my programming language IS TypeScript (not JS)

The the final output format generated by a compiler has absolutely nothing to do with whether the Language being compiled is typesafe or not. That would be like saying since Bytecode is not typesafe therefore Java is not a type safe language.


TypeScript is not the only way to achieve that.

Google closure compiler and JetBrains IDEs can use JSdoc to validate types. I have used those to a reasonable level of accuracy to document entire JavaScript projects where I cannot migrate to TypeScript.


Excellent. Rock on man. I never thought I'd be a Microsoft fan again, but after they invented TypeScript/VSCode, I love them so much I might move to Redmond.


Just Java? That seems bizarre. There's so many more languages now days than just Java. I would love for Swift to take off. Don't get me wrong there is a lot in the Java ecosystem, but it's age is showing. And it's not lightweight. If I'm trying to spawn processes quickly it's not Java. If I want to run 100 instances of something, the RAM is going to cost me. JS and other fast to boot languages are also much more adaptable to serverless.

Now I'm not saying there's no room for Java any more, but "just Java" meeeeeeh.




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

Search: