【Wepy 2.0】使用 Typescript

wepy 作者 npm 库地址

@wepy/compiler-typescript:包地址

安装

1
2
3
4
5
6
7
8
npm install @wepy/compiler-typescript --save-dev

# 下面几个插件需要安装,避免使用过程N多bug
npm install --save-dev @babel/plugin-proposal-class-properties
npm install --save-dev @babel/plugin-proposal-object-rest-spread
npm install --save-dev @babel/plugin-proposal-export-default-from
npm install --save-dev @babel/plugin-proposal-decorators
npm install --save-dev @babel/plugin-transform-typescript

配置

修改配置文件 wepy.config.js

1
2
3
4
5
const TypeScriptCompiler = require("@wepy/compiler-typescript");

module.exports = {
  plugins: [TypeScriptCompiler()],
};

添加 babel 插件支持

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
babel: {
  sourceMap: true,
  presets: [
    '@babel/preset-env',
    // '@babel/preset-typescript'
  ],
  plugins: [
    '@wepy/babel-plugin-import-regenerator',
    // add for typescript
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-object-rest-spread',
    '@babel/plugin-proposal-export-default-from',
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    '@babel/plugin-transform-typescript',
  ]
},

使用

wepy 文件中使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<script lang="typescript">
import wepy from '@wepy/core';

wepy.component({
  data: {},
  computed: {},
  methods: {},
  created() {},
});
</script>

可能遇到的问题

Wepy2.0使用Typescript-2023-11-30-14-50-06

没有安装 typescript

Fix:

1
npm install --save-dev typescript