Quasar CLI with Webpack - @quasar/app-webpack
支持TypeScript

Typescript支持在默认情况下不会添加到项目中(除非您在创建项目文件夹时选择了TS),但是可以按照本页上的指南轻松集成。

TIP

只有在创建新的Quasar项目时没有选择TypeScript支持时,才需要执行以下步骤。如果在项目创建时选择了TS选项,则已启用TypeScript支持。

安装TypeScript支持

为了支持TypeScript,您需要编辑/quasar.config.js

module.exports = function (ctx) {
  return {
    supportTS: true,
    ....
  }
}

然后在项目根目录下创建/tsconfig.json包含以下内容的文件:

{
  "extends": "@quasar/app-webpack/tsconfig-preset",
  "compilerOptions": {
    "baseUrl": "."
  }
}

现在可以开始在项目中使用TypeScript了。

TIP

请记住,必须将JavaScript文件的扩展名改为.ts,才能在其中编写TypeScript代码。要将TS代码写入组件,请更改脚本开头标记,如<script lang="ts">

WARNING

如果启用supportTS标志但未能添加tsconfig.json文件,应用程序将在编译时中断!

处理TS Webpack加载程序

在幕后,Quasar使用ts-loaderfork-ts-checker-webpack-plugin(由@quasar/app-webpack包提供)来管理ts文件。如果您需要为这些库提供自定义配置,您可以通过使supportTS属性如下所示来实现:

// quasar.config.js
module.exports = function (ctx) {
  return {
    supportTS: {
      tsLoaderConfig: {
        // `appendTsSuffixTo: [/\.vue$/]` and `transpileOnly: true` are added by default and cannot be overridden
        ...
      },
      tsCheckerConfig: {
        // `vue: true` is added by default and cannot be overridden
        ...
      }
    },
    ....
  }
}

Linting设置

首先添加所需的依赖项:

$ yarn add --dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

然后相应地更新ESLint配置,如下例所示:

// .eslintrc.js
const { resolve } = require('path');

module.exports = {
  // https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy
  // This option interrupts the configuration hierarchy at this file
  // Remove this if you have an higher level ESLint config file (it usually happens into a monorepos)
  root: true,

  // https://eslint.vuejs.org/user-guide/#how-to-use-custom-parser
  // Must use parserOptions instead of "parser" to allow vue-eslint-parser to keep working
  // `parser: 'vue-eslint-parser'` is already included with any 'plugin:vue/**' config and should be omitted
  parserOptions: {
    // https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration
    // https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#eslint
    // Needed to make the parser take into account 'vue' files
    extraFileExtensions: ['.vue'],
    parser: '@typescript-eslint/parser',
    project: resolve(__dirname, './tsconfig.json'),
    tsconfigRootDir: __dirname,
    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module', // Allows for the use of imports
  },

  // Rules order is important, please avoid shuffling them
  extends: [
    // Base ESLint recommended rules
    'eslint:recommended',

    // https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#usage
    // ESLint typescript rules
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    // consider disabling this class of rules if linting takes too long
    'plugin:@typescript-eslint/recommended-requiring-type-checking',

    // https://eslint.vuejs.org/rules/#priority-a-essential-error-prevention
    // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules
    'plugin:vue/essential',

    // --- ONLY WHEN USING PRETTIER ---
    // https://github.com/prettier/eslint-config-prettier#installation
    // usage with Prettier, provided by 'eslint-config-prettier'.
    'prettier',
    'prettier/@typescript-eslint',
    'prettier/vue',
  ],

  plugins: [
    // required to apply rules which need type information
    '@typescript-eslint',

    // https://eslint.vuejs.org/user-guide/#why-doesn-t-it-work-on-vue-file
    // required to lint *.vue files
    'vue',
  ],

  // add your custom rules here
  rules: {
    // others rules...

    // TypeScript
    'quotes': ['warn', 'single'],
    '@typescript-eslint/explicit-function-return-type': 'off',
  }
}

如果有任何问题,请阅读此示例所基于的typescript-eslint指南

最后一步,将yarn lint命令更新为lint.ts文件。

TIP

由于类型检查开销,TypeScript Linting非常慢,对于开发构建我们建议您在quasar.conf.js中禁用Webpack lint扩展

如果你安装了TypeScript linting并希望fork-ts-checker-webpack-plugin(由@quasar/app包提供)考虑到它, 则应使用tsCheckerConfig属性:

// quasar.config.js
module.exports = function (ctx) {
  return {
    supportTS: {
      tsCheckerConfig: {
        eslint: {
          enabled: true,
          files: './src/**/*.{ts,tsx,js,jsx,vue}'
        }
      }
    },
    ....
  }
}