Vue with Typescript can be type-checked without the use of Webpack or ts-loader

Starting from August 2018, there has been an option to compile TypeScript code using Babel while running type checking as a separate process.

I am curious if it is feasible to conduct type checking for custom file formats such as .vue with <script lang="ts"> sections without the need for Webpack/ts-loader? (It seems like in such scenarios, TypeScript would have to include support for preprocessing files of different formats).

Currently, I find myself maintaining a distinct webpack setup with ts-loader simply to compile a Vue.js project, when all I truly require is type-checking. As a result, this method feels more like a workaround and unnecessary complexity.

Answer №1

Using ts-loader alongside Webpack is completely acceptable in our setup at Vue.js, where we operate on a large-scale multi-page SPA architecture with Webpack as our primary bundler. It's not necessary to create a separate configuration specifically for ts-loader. Here's a glimpse of our Webpack configuration showcasing three instances of ts-loader:

const rules = [
    {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        include: [...PATHS.shared, path.join(__dirname, 'node_modules')],
        options: {
            transpileOnly: isDev,
            appendTsSuffixTo: [/\.vue$/],
            instance: 'common',
            configFile: path.join(__dirname, 'tsconfig.json')
        }
    },
    {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        include: [PATHS.app1],
        options: {
            transpileOnly: isDev,
            appendTsSuffixTo: [/\.vue$/],
            instance: 'app1',
            configFile: path.join(PATHS.app1, 'tsconfig.json')
        }
    },
    {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        include: [PATHS.app2],
        options: {
            transpileOnly: isDev,
            appendTsSuffixTo: [/\.vue$/],
            instance: 'app2',
            configFile: path.join(PATHS.app2, 'tsconfig.json')
        }
    }
];

In response to your query, while I have found success using @babel/preset-typescript, it's important to note that we refrain from utilizing .vue files due to issues encountered during their processing which can be seen here. Note: During development, we set transpileOnly to false.

If feasible, transitioning to the class syntax combined with @Component decorator and integrating vue-template-loader could facilitate a switch to Babel + TypeScript. While making this transition, be mindful of limitations such as lack of support for features like const enums, namespaces, etc.

It should be noted that opting for ts-loader instead of @babel/preset-typescript comes with its own set of advantages. When paired with Webpack, ts-loader offers more flexibility. If typed JSX (TSX) compatibility is needed with Vue.js, then utilizing the babel approach might be more suitable. However, if you are working with .vue files, the difference may not be significant.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using React and Typescript: How do I properly type a button that occasionally uses "as={Link}"?

I've encountered a scenario where I have a versatile button component that can either function as a button or transform into a link for enhanced user experience by using to={Link}. The challenge arises when Typescript always interprets the button as a ...

Master the art of iterating through an Object in TypeScript

I need help with looping through an Object in TypeScript. While the following examples work in JavaScript, I understand why they pose a problem in TypeScript. Unfortunately, I am struggling to find the correct approach to solve this issue. Am I approaching ...

A result of the array's elements' index will form an object

Here is a straightforward example for you to work with. I am trying to retrieve the index of each Test component. However, the key value is returning an object. Since I'm not very well-versed in Reactjs, could someone guide me on how to get the index ...

How can I retrieve only the pertinent information stored in Firestore?

I'm struggling to filter out only the data relevant to a specific "userId" from Firestore, as currently everything in the database is being printed. I've attempted to make changes based on advice I received but it hasn't resulted in any impr ...

Transferring AgGrid context in a functional React component

I have been working on a component that utilizes AgGrid to display a table, with the data sourced from a Redux selector. My goal is to include a button within a cell in the table that triggers an action based on the specific row's data. However, I a ...

Apologies, the module "@org-name/package-name" could not be located

I've hit a roadblock for the past few days. My goal is to create a new npm package that wraps an API I've been developing. When bundling the package, everything seems to be fine in the /dist folder. However, when attempting to test it in a front ...

Execute --runTestsByPath on two or more distinct paths

Currently, I am utilizing the jest cli for running my tests. Jest offers a useful cli option known as --runTestsByPath, which allows me to specify the locations of my tests. Despite having unit tests spread out in various directories within my repository, ...

TypeScript class that utilizes internal functions to implement another class

In my journey of exploration, I decided to try implementing a class within another class in TypeScript. Here is the code snippet I came up with and tested on the Playground link: class A { private f() { console.log("f"); } public g() { console.lo ...

Is the removal of the Vue-Router link happening when you click on the top app bar icon in Google Material

Review of the following code snippet: <!DOCTYPE html> <html> <head> <title>test</title> <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='vie ...

Increase progress by pressing a button

I've implemented the NgCircleProgressModule library to display a circle with a percentage value in the center. It seems that the progress value remains static and only updates when the buttons are clicked. Clicking on the 25% button within the circl ...

The onmouseup event is not triggering in Vue.js 2

Show me the full code example here: https://github.com/kenpeter/test_vue_simple_audio_1 Attaching @onmouseup to the input range appears to have an issue. When I drag the slider, the function progressChange does not seem to be triggered. <input type ...

Converting October website pages' formatting into JSON for a Vue.js application

In order to create a Vue menu in OctoberCMS, I have developed a plugin that extracts the page structure from October pages into JSON data while maintaining the indentation of pages and subpages. Referring to this post: How to get static page dropdown in O ...

Refreshing Form after Saving in Angular 4

After clicking the Save button, I want to reset the form (addUserForm). I created a modal with a form to input user data. This form serves for both editing existing data and creating new data, with the flag 'Create' or 'Update' differen ...

Nuxt project encountering issues with loading JS files

I've been integrating a bootstrap template into my Nuxt project but seem to be facing some challenges with loading the necessary scripts. I've attempted to import the scripts into my nuxt.config.js file in a couple of ways: 1.) Initially, I tri ...

Rebooting: Relaunching NodeJS server following deployment

I recently launched my inaugural Laravel Forge website featuring a server-side rendered VueJS 2.0 application. Although I've implemented quick deployment and a daemon to ensure constant operation, I've encountered an issue during deployments. Th ...

The ngAfterViewInit lifecycle hook does not get triggered when placed within ng-content

The ngAfterViewInit lifecycle hook isn't triggered for a Component that is transcluded into another component using <ng-content>, as shown below: <app-container [showContent]="showContentContainer"> <app-input></app-input> ...

Guide on manipulating props in VueJS

Building an application using Vue2 involves passing data from a parent component to a child component through v-for. Here's how the parent component looks: <template> <div class="row" v-for="(item, index) in variables" ...

Is there a way to create optional sections within a reusable component's template in a Vue 3 application?

Recently, I've been immersed in developing a Single Page Application (SPA) using the powerful combination of Vue 3, TypeScript, and integrating The Movie Database (TMDB) API. One interesting challenge I encountered was with my versatile movie card co ...

The presence of catchError() within the pipe() function will display an error specifically at the .subscribe stage

I encountered an issue while trying to handle errors for a method in Angular. After adding a catchError check using the .pipe() method, I noticed that the variable roomId was marked with a red squiggly line. The error message read: TS2345: Argument of type ...

Bring in all iterable arrays from TypeScript using the import statement

How can TypeScript be used to access multiple classes from different files as an array? Consider the following folder structure: ├── index.ts └── models ├── index.ts ├── image.entity.ts └── user.entity.ts In the ...