I still don't understand what pulls developers to Typescript. The examples given show major flaws in this (duck)type system. That it works when you don't use the 'any' type and when you know how to avoid all the edge cases and workarounds, I know. But when I see an example like this: const shasta = new Cat("Maine Coon") printDog(shasta) > Dog:Maine Coon I see a huge red flag. Coming from C/C++ it looks like a joke. W…
It just means that structural types are the default and you need to opt in to nominal types, when necessary: https://www.typescriptlang.org/play/?ssl=13&ssc=1&pln=13&pc=... (there are other ways as well) In something like Java, it's the opposite: you get nominal types by default and you need to opt in to structural types (via interfaces). Because TS is built on a duck typed language, tons of existing libraries would…
TypeScript’s quirks: How inconsistencies make the language more complex
81–90 of 216 posts
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#82Ok since we have a lot of TS people here maybe someone can help me out. Let's say I have a simple interface with a few fields. I want to make sure incoming JSON conforms to that interface. I realize at runtime the interface isn't there, but is there some tool I can use to compile a utility to check JSON against an interface without having to write a duplicative JSONSchema or something else?
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#83Ok since we have a lot of TS people here maybe someone can help me out. Let's say I have a simple interface with a few fields. I want to make sure incoming JSON conforms to that interface. I realize at runtime the interface isn't there, but is there some tool I can use to compile a utility to check JSON against an interface without having to write a duplicative JSONSchema or something else?
https://github.com/gristlabs/ts-interface-builder
https://github.com/gristlabs/ts-interface-checker
Example: https://github.com/alangpierce/sucrase/pull/468/files
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#84Earlier quoted context omitted.
I’m not a very skilled Haskell programmer, but every time I’ve tried to parse JSON in it I’ve felt a strong urge to switch back to Python or JavaScript. There are libraries that make it easier but I think it’s always going to be hardly to get started with JSON APIs in a language with a powerful and strict type system. Of course, that extra up-front effort might be worth it in the long run due to the benefits of type…
Out of curiousity, what exactly was the difficulty with decoding JSON in Haskell/
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#85Earlier quoted context omitted.
Out of curiousity, what exactly was the difficulty with decoding JSON in Haskell/
a quick search brings up a library, not sure why you would have trouble, other than maybe experience? https://github.com/bos/aeson
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#86Typescript is great but it has its evils because it’s a superset of JavaScript. I really hope we get a modern language like c# where the types are guaranteed compile + runtime and you can’t any-ignore problems. The more I use typescript I realize I want a more sound and stricter language. Like no prototype overriding at runtime.
Noooo! Go away with those ideas! JavaScript is great because it’s not strict. That’s the flexibility that allows for speed. I have like 30 different APIs I implement at work, but I only pull out a few attributes from each. I don’t want to go to the moon and back creating types for all this crap, I’ll have more than a 100 different types to model because of all the nested data and convoluted structures the APIs return…
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#871) Be type safe enough to guarantee practically bug-free code (at least with regard to types, you can always have logic bugs)
2) Actually achieve what I want to do in less than a day
3) Upgrade easily when a new version of the language/tooling is released
The only thing that's missing is indeed better nominal typing so that we can more easily discriminate between logically separate instances of strings and numbers. For objects it's actually good enough to qualify for "perfect".
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#88I don’t agree with the article’s claim that discriminated unions are just a JS compat feature. Many APIs type JSON serialised entities using a string property and this feature makes it very easy to write interfaces and code to work with them.
This. My network code uses them everywhere on the client side. It's frankly amazing. Your server side code should have runtime validation though. You can't and shouldn't rely on TypeScript there for security reasons.
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#89The following is purely anecdotally, my opinions is solely based on my personal experience with the language. As someone who learned TypeScript just by using it and without any serious "study", I'll have to disagree the basic premise of the article. I already knew about almost everything the article mentioned and nothing really seemed weird to me at the time. The only point that I did not know about is type narrowing…
> TS can't know about your "classes" - they're a runtime thing. Treating them the same as interfaces can make this much nicer. No–this is a very surprising and unsound choice and not at all what you would expect if you previously saw that TypeScript lifts classes into types. > This way you can have type safety without losing JS features. IMHO, you can't get type safety out of an unsound type system. (Which TypeScript…
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#90Earlier quoted context omitted.
TypeScript’s goal isn’t to be sound. Its goal is to be a balance between type-safety and effort. Full type safety would require a lot of runtime checks injected into the final JavaScript, which TypeScript intentionally avoids. JavaScript will never be fully type-safe without fundamental changes, which will probably never happen.
I understand that, which is why I said 'Which TypeScript is [unsound], by choice'. Disagree that full type safety would require a lot of runtime checks. That's not the experience I've had in ReasonML.