3 ms·
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 par
by WClayFerguson 6y ago
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.
- 29athrowaway 6y agoThat 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.
- WClayFerguson 6y agoProxy 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.
- 29athrowaway 6y agoAnd 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.
- WClayFerguson 6y agoYou 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.
- 29athrowaway 6y agoTypeScript 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.
- WClayFerguson 6y agoExcellent. 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.