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

MySQL column to row conversion, method of merging fields (must read)

Data Sheet: Column to row: using max(case when th...

Analysis of the principle of Vue nextTick

Table of contents Event Loop miscroTask (microtas...

5 issues you should pay attention to when making a web page

1. Color matching problem <br />A web page s...

Detailed explanation of overlay network in Docker

Translated from Docker official documentation, or...

CSS to achieve the sticky effect of two balls intersecting sample code

This is an effect created purely using CSS. To pu...

Javascript tree menu (11 items)

1. dhtmlxTree dHTMLxTree is a feature-rich Tree M...

Linux platform mysql enable remote login

During the development process, I often encounter...

Examples of 4 methods for inserting large amounts of data in MySQL

Preface This article mainly introduces 4 methods ...

JavaScript adds event listeners to event delegation in batches. Detailed process

1. What is event delegation? Event delegation: Ut...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

Steps for Docker to build a private warehouse Harbor

Harbor Harbor is an open source solution for buil...

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

Tutorial on how to deploy LNMP and enable HTTPS service

What is LNMP: Linux+Nginx+Mysql+(php-fpm,php-mysq...

Share 8 very useful CSS development tools

CSS3 Patterns Gallery This CSS3 pattern library s...