Detailed steps for developing WeChat mini-programs using Typescript

Detailed steps for developing WeChat mini-programs using Typescript

We don't need to elaborate too much on the advantages of Typescript. If you are interested, you can refer to (https://www.typescriptlang.org/). Today I will share with you how to use Typescript in the development of WeChat mini-programs (or other similar mini-programs).

There are two situations for this. The simplest way is to select the Typescript option when creating a project, as shown in the figure below. But please note that this option is only available when you select "Use no cloud service", and the other Mini Program Cloud Base is not supported. This may be because the development tools have not caught up yet. I hope it can be selected by default in the future.

So the question is, what should I do if I choose the first Mini Program Cloud Base, or if I have a project before and now want to use Typescript? It’s not difficult, please refer to the steps I summarized below.

Step 1: Make sure your project has a package.json file and add the following two lines. The rest can be different. If the file does not exist, generate it using the npm init command. After modifying the file, run the npm install command to generate local dependencies.

The second step is to add a tsconfig.json file to your project with the following content

{

"compilerOptions": {

"strictNullChecks": true,

"noImplicitAny": true,

"module": "CommonJS",

"target": "ES5",

"allowJs": false,

"experimentalDecorators": true,

"noImplicitThis": true,

"noImplicitReturns": true,

"alwaysStrict": true,

"inlineSourceMap": true,

"inlineSources": true,

"noFallthroughCasesInSwitch": true,

"noUnusedLocals": true,

"noUnusedParameters": true,

"strict": true,

"removeComments": true,

"pretty": true,

"strictPropertyInitialization": true,

"lib": [

"es5"

],

"typeRoots": [

"./typings"

]

},

"include": [

"./**/*.ts"

],

"exclude": [

"node_modules"

]

}

The third step is to download the following compressed package, decompress it, and put it in the root directory of the project.

http://xiazai.jb51.net/202101/yuanma/typings_jb51.rar

The file here is the type definition file d.ts officially provided by Tencent

Step 4: Modify the project.config.json file and add preprocessing commands

"scripts": {

"beforeCompile": "npm run tsc",

"beforePreview": "npm run tsc",

"beforeUpload": "npm run tsc"

}, 

Step 5. Make sure the preprocessing command is enabled in the WeChat Developer Tools.

Done. Now you can happily use Typescript to develop WeChat applets. I also recommend using VS Code for direct development. The "WeChat Developer Tools" is only used for compilation and publishing. This development experience is really smooth and silky smooth. I will share this content in the next article when I have time.

This concludes this article on the detailed steps of using Typescript to develop WeChat applets. For more content related to developing WeChat applets with Typescript, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • TypeScript generic parameter default types and new strict compilation option
  • Typescript 3.9 common new features list (recommended)
  • JS Decorator Pattern and TypeScript Decorators
  • Detailed explanation of type protection in TypeScript
  • Detailed explanation of how to use This in Typescript
  • Use TypeScript to quickly develop a CLI implementation example
  • TypeScript Advanced Types You Don’t Know (Summary)
  • 7 useful new TypeScript features

<<:  Complete steps to use mock.js in Vue project

>>:  Two ways to connect WeChat mini program to Tencent Maps

Recommend

Pure CSS to achieve left and right drag to change the layout size

Utilize the browser's non- overflow:auto elem...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

Detailed explanation of the usage of compose function and pipe function in JS

Table of contents compose function Array.prototyp...

How to reset MySQL root password under Windows

Today I found that WordPress could not connect to...

Multiple ways to change the SELECT options in an HTML drop-down box

After the form is submitted, the returned HTML pag...

Vue+Element realizes paging effect

This article example shares the specific code of ...

Mysql database scheduled backup script sharing

BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...

How to write the parent and child directories of HTML relative paths

How to indicate the parent directory ../ represent...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...

MySQL Daemon failed to start error solution

MySQL Daemon failed to start error solution A few...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...

Vue.js implements timeline function

This article shares the specific code of Vue.js t...

js object to achieve data paging effect

This article example shares the specific code of ...

jQuery realizes image highlighting

It is very common to highlight images on a page. ...