Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
JavaScript is not, arguably, suitable for large complex applications, so the idea behind an additional script was to make it a complementary language that can be “scalable.” Let’s address the core differences between those languages, features of TypeScript, and why many people think that TypeScript is going to overrun JavaScript and take over the world (yeah, right).
JavaScript is not (arguably) suitable for large complex applications, so the idea behind an additional script was to make it a complementary language that can be “scalable.” However, as Kyle Simpson put it in one of his famous keynotes at the JavaScript conference when addressing issues with TypeScript was that ‘TypeScript solves problems most of us don’t really have in the ways we don’t really like.’ Nevertheless, TypeScript is a nice addition to the ecosystem, but there’s still a cost to TypeScript because you choosing to ‘marry yourself to something that will never make its way into JavaScript because it limits your future possibilities’. TypeScript is a nice addition to JavaScript, but is it really a stand-alone technology? This and other questions, we’re going to address in this article.
TypeScript is a superset of JavaScript, a statically compiled language to write simple JavaScript code. TypeScript provides optional static typing, classes, and interface, and is thought to have better code structuring and object-oriented programming techniques. TypeScript also allows for better development time tool support and can extend the language beyond the standard decorators. Moreover, it can be converted to plain JavaScript, of course.
There are two ways you can start using TypeScript: you can get TypeScript tools either via npm or by installing TypeScript’s Visual Studio plugins (in case you’re using Visual Studio 2017, then TypeScript is there by default). For npm, use this: > npm install -g typescript For everything else: download it here.
The main TypeScript features are:
The thing with TypeScript is that you can really achieve all those things that TypeScript is good for with Flow for JavaScript. Flow and TypeScript are very similar products, however, there are important differences.
One of the main problems with Flow is in IDE integrations. Language server is a work in progress, some IDEs, for example, use the CLI and require saving the file to run the type-check, refactoring is still in alpha version, only type information on hover, and sketchy go-to definition. As opposed to that, TypeScript IDE is really top-notch: language server, refactorings are built-in, type and typedoc information on hover, and snappy go-to-definition.
The other differences are in autocomplete, which in TS feels almost instantaneous and reliable, whereas, in Flow, you often have to wait for a second or more of a delay.
Some of the unique features of TypeScript are autocomplete for object construction, declarable this in functions, a large library of typings, more flexible type mapping via iteration, and namespacing. Flow has variance, testing potential code-paths when types are not declared for maximum inference, $Diff<A, B> type.
Among other differences: in TS there’s support for legacy proposal and for extending built-in types, however no support for nullish coalescing proposal; in Flow, however, only parsing of legacy proposal and no type-checking, no support for extending built-in types, but support for nullish coalescing proposal.
JavaScript
function checkArgsAndDoStaff(arg1, arg2, arg3) { if (typeof arg1 !== 'string' || typeof arg1 !== 'boolean') { throw new Error('arg1 is not assignable to parameter of type string | boolean'); } if (typeof arg2 !== 'object') { throw new Error('arg2 must be an object'); } if (typeof arg2.a !== 'string') { throw new Error('arg2 must be a string'); } if (! b in arg2) { throw new Error('property b is missing in arg2'); } // do staff } // we need to check whether the result of checkArgsAndDoSomeStaff is boolean // and errors will be received during runtime
TypeScript
function checkArgsAndDoStaff(arg1: string | boolean, arg2: { a: string, b: unknown } ): boolean { // do some staff } // Flow function checkArgsAndDoStaff(arg1: string | boolean, arg2: { a: string, b: mixed } ): boolean { // do some staff }
Conclusion:
Next time, we’ll be covering TypeScript interview questions, meanwhile, please see — for JavaScript Interview Questions: JS Interview Part 1, Part 2, Part 3