Web development: TypeScript in Vue projects

Share your love

Those who develop Vue projects usually use JavaScript and not TypeScript. A quick search on GitHub reveals that there are approximately 170,000 projects with JavaScript and 8,500 projects with TypeScript that have Vue in their name or description. It is not surprising. For example, tools such as the Vue CLI are defined as a quasi-standard, which create a project with Vue 2, Babel, ESLint and JavaScript in the standard settings. The documentation for Vue 2 and 3 also only speaks of TypeScript in selected areas.

Basically, it is not a problem to create a Vue project with TypeScript. Vue 3 is a very grateful web framework in this regard, as the core of this version itself is implemented in TypeScript. But even with Vue 2, the question is not whether TypeScript works at all, but rather whether the language fits the project requirements, the available resources and your own taste – regardless of whether it is about the choice of the Options API, the Class- Style Vue Components or the Composition API is all about.

TypeScript uses concepts such as object orientation and type inference to describe data. The strictly typed script language can deal with generic data types, for example. Since TypeScript is a superset of the JavaScript programming language, it compiles to it to run in the browser. The data types of the type system promote readable code, type safety, improved debugging and better tool support. The language has been available for almost a decade, and a lot has happened in that time. Numerous development environments and text editors now support TypeScript and its ecosystem.

Read Also   EU data protectionists: The digital euro should be as anonymous as cash

TypeScript can be used with both Vue 2.x and the new version 3.x. The Vue team has re-implemented the core libraries in version 3 with TypeScript, but support for TypeScript in your own projects is optional, as with version 2. All variants of the API models such as the Options API and the Components API support TypeScript, and there is also the Class-based Components Style for use with TypeScript. Later in this article, we take a closer look at the differences between these APIs in the context of JavaScript and TypeScript.

For some time now, Vue has been supporting the Delivery with official type declarations for TypeScript the programming language officially. The necessary configuration for TypeScript support can either be created automatically when creating a Vue project or added manually afterwards. No further tooling is initially necessary. The type declarations are published as a package and TypeScript knows how to resolve these declarations in npm packages, making TypeScript usable directly.

The use of an editor or an IDE with TypeScript support is recommended. Due to the integration in most development environments, this is also no longer a major hurdle, for example TypeScript support in Visual Studio Code or WebStorm is part of the game. In addition, there are extensions for integrating Vue support like the Vetur Extension recommended, which Vue developers should already be familiar with.

Before using TypeScript, the question arises as to what advantages and disadvantages this brings. Since TypeScript must be fully interoperable with JavaScript, the typing system can be quite complicated if used correctly. Another argument against TypeScript is that, unlike JavaScript, it requires compilation. However, this is often a bogus argument, because most JavaScript applications nowadays require a build step – whether when used with Gulp, Grunt, Webpack, Rollup, Babel or Closure. For Vue projects this is also available in most cases and can only be extended for TypeScript. In addition, there must be no excessive feeling of security just because TypeScript checks the data types, as this does not rule out bugs.

Read Also   Laser Detection to Clean Dust: The New Dyson V15 Detect Vacuum

The advantages of TypeScript – regardless of the use with Vue – include type definitions, more understandable compiler messages and extensive tool support. The combination of TypeScript and Vue 2.x has the greatest advantage in terms of the class-style syntax for Vue components. Components can be created as normal classes with regular properties, properties and methods for watchers and events, for example. This is not only helpful for those who come from object-oriented programming languages ​​like C # or Java. This makes many feel more “at home” than in the Vue syntax of the Options API. In Vue 3.x, the combination of the Composition API and TypeScript looks very promising.

Article Source

Share your love