DEV Community

Cover image for A Type System

A Type System

Kinanee Samson on July 14, 2021

TypeScript in my opinion will always remain a superhero large scale application development tool, TypeScript comes loaded with a great type system ...
Collapse
 
peerreynders profile image
peerreynders

Just know that TypeScript doesn't have your back as much as other options:

A Note on Soundness

TypeScript’s type system allows certain operations that can’t be known at compile-time to be safe. When a type system has this property, it is said to not be “sound”.

Compare with Rescript:

Has no pitfalls, aka the type system is "sound" (the types will always be correct). E.g. If a type isn't marked as nullable, its value will never lie and let through some undefined value silently. ReScript code has no null/undefined errors.

Collapse
 
mindplay profile image
Rasmus Schultz

Also check out Hegel - a static type-checker for JS that attempts to achieve soundness, which also enables far more powerful inference.

Obviously, this project is new, and has to basically "start over" on many of the things that TS added over the years. To avoid that, they do try to leverage existing .d.ts files, but this only seems to work for the most basic type-definitions right now, and man do they have their work cut out for them.

Still, this is definitely worth a look - I would strongly encourage anyone to at least spend 20 minutes poking around in the online playground to see what this is about. If you don't already know something like Elm or ReScript, you will be surprised by the safety you get from sound type-checking, and with a much smaller amount of manually type-hinting compared with TS.

This project is so overlooked, and really deserves a lot more attention than it gets. I think maybe some people are already fatigued by the constant growing complexity of TS, and my own early reaction was something like "ugh, another type-checker?" - but this really is a whole different ball game. Don't skip this one! 🙂

Collapse
 
peerreynders profile image
peerreynders

Given

Hegel is only JavaScript with types

and

an advanced static type checker

(i.e. Hegel is a tool rather than a language) it seems that comment syntax should be a "rather sooner than later" feature

  • to mitigate the risk of adoption
  • to make potentially lengthy type checking passes optional for regular red-green-refactor cycles. While this may sound weird from the static typing perspective this is what seems to be driving JSDoc TS adoption (TypeScript as a tool rather than a language or compiler).
Collapse
 
drumstix42 profile image
Mark

Rescript has a lot of other syntax changes that go along with it, many of them opinionated at that. There's enough differences beyond just Typing that prevent me from ever wanting to use it.

Collapse
 
kalashin1 profile image
Kinanee Samson

Hmmm... Thanks for the heads up.. I think i need to get familiar with Rescript and check more about "soundness"

Collapse
 
peerreynders profile image
peerreynders • Edited

Look, given how the JavaScript ecosystem works (i.e. tendency to be heavy on external dependencies) it's not all sunshine and roses in the ReScript world because JavaScript Interop is a topic in and onto itself.

I still believe that TypeScript adoption has less to do with the sincere desire (or need) to adopt static typing but more with VS Code's TypeScript support and the associated developer experience.

TypeScript knowingly limited the "type safety" potential by basing its syntax on JavaScript (prioritizing JavaScript interoperability) but then promptly introduced its own non-compatible syntax requiring transpilation.

At this point one cannot get around TypeScript. However I think it's a good idea to get familiar with JSDoc TypeScript. That way one can choose to author in JavaScript, not having to transpile all the time but simply run TypeScript occasionally as a super-linter.

Personally I still view TypeScript as a missed opportunity. Given the cost of compilation (and having to learn additional syntax) there was a real opportunity to use modern static typing (e.g. Rust vs C/C++).

Quote

TypeScript began its life as an attempt to bring traditional object-oriented types to JavaScript so that the programmers at Microsoft could bring traditional object-oriented programs to the web.

Microsoft introduced F# in 2005 and TypeScript didn't appear until 7 years later in 2012 - but ultimately the C# way of working and thinking prevaled (given that JavaScript dates back to 1995 I don't see 17 years worth of improvement in TypeScript).

See also Here is why you might NOT want to use TypeScript, The Trouble with TypeScript.

Collapse
 
asmyshlyaev177 profile image
Alex • Edited

Just TS took enough time, but solve only simplest errors, those can be easily be caught by tests.
Most complex and time consuming errors come from misunderstanding of requirements, trying to use libraries wrong way, just bad code and no refactoring.

Mostly TS helps with autocomplete, very little benefits in long run.

researchgate.net/publication/25963...
medium.com/javascript-scene/the-sh...

Collapse
 
kalashin1 profile image
Kinanee Samson

In my opinion TypeScript provides much more than auto-complete. TypeScript is a large scale application tool that is employed by various software engineers and companies to build large scale web apps. TypeScript also aids a lot with documentation, throw in jsdoc to the mix and you have a great developer experience. Outside this TypeScript allows you to use out of the box features that are not yet supported in the browser... All the reasons for errors you gave above are actually true, if you have consumed any popular library you work with on JavaScript with TypeScript, you will understand the role TypeScript plays. The numbers speaks for themselves. Just check up how many people is using TypeScript.

Collapse
 
asmyshlyaev177 profile image
Alex

How many people use something doesn't mean it's automatically best option.
I remember GraphQL client for react, it's written on TS, doesn't change that is buggy as hell.

Another great article
medium.com/javascript-scene/the-ty...

Still need to learn pure JS, even weird parts. Still need to test code, testing in my experience makes huge impact, code review also.
Time and resources limited, time spent on fighting TS is time not spent on tests and refactoring, i see it a few times, didn't see other way around yet.

Collapse
 
menard_codes profile image
Menard Maranan

I've been considering learning TypeScript, and thankfully I found this article. Definitely will use this as a guide, and bookmarked it too! Thanks for the info!

Collapse
 
kalashin1 profile image
Kinanee Samson

i am glad that you found it useful bro