Table of Contents
Overview" >Overview
Reformation problem recording and analysis" >Reformation problem recording and analysis
VSCode related" >VSCode related
TypeScript related" >TypeScript related
Object attribute assignment error" >Object attribute assignment error
Window object attribute assignment error" >Window object attribute assignment error
ES2015 Object's new method on the prototype chain reports an error " >ES2015 Object's new method on the prototype chain reports an error
ES2015 new data structure Map initialization error " >ES2015 new data structure Map initialization error
ES2015新增的Promise使用报错" >ES2015新增的Promise使用报错
SetTimeout使用报错" >SetTimeout使用报错
模块引用和导出报错" >模块引用和导出报错
总结" >总结
Home Web Front-end JS Tutorial TypeScrip reshaping problems and solutions

TypeScrip reshaping problems and solutions

Jul 25, 2018 am 10:08 AM
javascript typescript

The content shared with you in this article is about TypeScript transformation problems and solutions. The content is very detailed. Next, let’s take a look at the specific content. I hope it can help everyone.

Overview

Since this transformation project is a basic service package published through NPM, the goal of this transformation using TypeScript is to remove the Babel family Bucket, reduce package size, and add strong type constraints to avoid possible problems in future development.

This transformation uses TypeScript v2.9.2 and Webpack v4.16.0 for packaging and compilation. The development tool uses VSCode and uses the Chinese language pack. The expected goal is to directly compile TypeScript code into ES5 code through the loader.

Some of the issues involved in this article are TypeScript configuration and usage issues, and some are issues related to the configuration of VSCode itself.

Reformation problem recording and analysis

"Unable to find relevant module" error

In the project, if we use webpack.alias, we may be prompted that the module cannot be found.

The specific errors are as follows:

终端编译报错:TS2307: Cannot find module '_utils/index'.
编辑器报错:[ts]找不到模块“_utils/index”。
Copy after login

This is caused by the editor being unable to read the corresponding alias information.

At this point we need to check whether the corresponding module exists. If it is confirmed that the module exists and no error is reported during terminal compilation, but only an error is reported by the editor, it is because the editor cannot read the webpack configuration and we need to add additional configuration.

Solution: In addition to configuring webpack.alias, you also need to configure the corresponding tsconfig.json. The specific configuration is as follows:

"compilerOptions": {
    "baseUrl": ".",
    "paths": {
        "_util/*": [
            "src/core/utils/*"
        ]
    }
}
Copy after login

Note: If If you still get an error after configuring tsconfig.json, you need to restart VSCode. It is speculated that VSCode only reads relevant configuration information when the project is loaded. The same goes for jsconfig.json in JavaScript projects.

Object attribute assignment error

In JavaScript, we often declare an empty object , and then assign a value to this attribute. However, this operation will cause an error when placed in TypeScript:

let a = {};

a.b = 1;
// 终端编译报错:TS2339: Property 'b' does not exist on type '{}'.
// 编辑器报错:[ts] 类型“{}”上不存在属性“b”。
Copy after login

This is because TypeScript does not allow adding undeclared attributes.

Therefore, we have two ways to solve this error:

  1. Add attribute definitions to the object (recommended). The specific method is: let a = {b: void 0};. This method can fundamentally solve the current problem and avoid the problem of objects being randomly assigned.

  2. Add any attribute to the a object (emergency). The specific method is: let a: any = {};. This method allows TypeScript to ignore this object during type checking, so that no error is reported during compilation. This method is suitable for large amounts of old code transformation.

Window object attribute assignment error

Similar to the previous situation, when we assign a non-existent attribute to an object, an edit will appear The compiler and compiler reported an error:

window.a = 1;
// 终端编译报错:TS2339: Property 'a' does not exist on type 'Window'.
// 编辑器报错:[ts] 类型“Window”上不存在属性“a”。
Copy after login

This is also caused by the fact that TypeScript does not allow adding undeclared attributes.

Since we have no way to declare the value of the windows attribute (or it is very difficult), we need to solve it in the following way:

  1. We use in windows Add a type conversion, that is (window as any).a = 1;. This ensures that there will be no errors in the editor and during compilation. However, this method is only recommended for the renovation of old projects. We should try to avoid adding attributes to the window object. Data should be accessed through a global data manager.

ES2015 Object's new method on the prototype chain reports an error

In the project, some ES2015 new methods on the Object prototype chain are used Added methods, such as Object.assign and Object.values, etc., the compilation will fail at this time, and VSCode will prompt an error:

终端编译报错:TS2339: Property 'assign' does not exist on type 'ObjectConstructor'.
编辑器报错:[ts] 类型“ObjectConstructor”上不存在属性“assign”。
Copy after login

This is because we are The target specified in tsconfig.json is ES5, and TypeScript does not have the relevant polyfill, so we cannot use the new methods in ES2015.

Solution: You can use the related methods in the lodash tool set. You need to install lodash.assign and @types/lodash.assign during installation. And lodash.assign is a CMD specification package and needs to be introduced through import _assign = require('lodash.assing');.

ES2015 new data structure Map initialization error

When transforming ES2015 code into TypeScript code, if you use the new Map type in ES2015, then An error will be reported when compiling in the editor or terminal:

终端编译报错:TS2693: 'Map' only refers to a type, but is being used as a value here.
编辑器报错报错:[ts] “Map”仅表示类型,但在此处却作为值使用。
Copy after login

This is because TypeScript does not provide relevant data types and there is no corresponding polyfill.

Therefore, we have three ideas to solve this problem:

  1. tsconfig.json配置中的target属性改为es6,即输出符合ES2015规范的代码。因为ES2015存在全局的Promise对象,因此编译和编辑器都不会报错。该方法优点为配置简单,无需改动代码,缺点为需要高级浏览器的支持或者Babel全家桶的支持。

  2. 舍弃Map类型,改用Object进行替代。这种改造比较费时费力,适用于工作量较小和不愿意引入其他文件的场景。

  3. 自行实现或者安装一个Map包。这种方法改造成本较小,缺点就是会引入额外的代码或者包,并且代码效率无法保证。例如ts-maptypescript-map,这两个包的查找效率都是o(n),低于原生类型的Map。因此推荐自己使用Object实现一个简单的Map,具体实现方式可以去网上找相关的Map原理分析与实践(大致原理为使用多个Object,存储不同类型元素时使用不同容器,避免类型转换问题)。

ES2015新增的Promise使用报错

将ES2015的代码改造成为TypeScript代码时,如果你使用了ES2015的新增的Promise类型,那在编辑器还是终端编译编译时都会报错:

终端编译报错: TS2693: 'Promise' only refers to a type, but is being used as a value here.
编辑器报错:[ts] “Promise”仅表示类型,但在此处却作为值使用。
Copy after login

这是由于TypeScript并没有提供Promise数据类型,也没有对应的polyfill。

因此,我们解决这个问题的思路仍然有三种:

  1. tsconfig.json配置文件配置中的target属性改为es6,即输出符合ES2015规范的代码。因为ES2015存在全局的Promise对象,因此编译和编辑器都不会报错。该方法优点为配置简单,无需改动代码,缺点为需要高级浏览器的支持或者Babel全家桶的支持。

  2. 引入一个Promise库,如bluebird等比较知名的Promise库。在安装bluebird时需要同时安装@types/bluebird声明文件。缺点就是引入的Promise库较大,而且如果你的库作为一个基础库时,可能会与其他的调用方的Promise库产生冲突。

  3. tsconfig.json配置文件中增加lib。此方法的原理是让TypeScript编译时引用外部的Promise对象,因此在编译时不会报错。此方式优点是不会引入任何其他代码,但是缺点是一定要保证在引用此库的前提下,一定存在Promise对象。具体配置如下:

    "compilerOptions": {
        "lib": ["es2015.promise"]
    }
    Copy after login

SetTimeout使用报错

将ES2015代码改造成TypeScript代码时,如果使用了setTimeout和setInterval函数时,可能会出现无法找到该函数的报错:

终端编译报错:TS2304: Cannot find name 'setTimeout'.
编辑器报错:[ts] 找不到名称“setTimeout”。
Copy after login

这是由于编辑器和编译时不知道当前代码运行环境导致的。

因此,我们解决这个问题的思路有两种:

  1. tsconfig.json配置文件中增加lib。让TypeScript能够知道当前的代码容器。具体示例如下:

    "compilerOptions": {
        "lib": ["dom"]
    }
    Copy after login
  2. 安装@types/node。该方法适用于node环境下或者采用webpack打包时可以引入node代码。该方法直接通过npm install @types/node即可安装完成,解决报错问题。

模块引用和导出报错

在ES2015的代码中,我们可以通过@babel/plugin-proposal-export-default-from插件来直接导出引入的文件,具体示例如下:

export Session from './session'; // 报错
export * from '_models/read-item'; // 不报错
Copy after login

而在TypeScript中,这种写法是会报错的:

终端编译报错:TS1128: Declaration or statement expected.
编辑器报错:[ts] 应为声明或语句。
Copy after login

这是由于两者的模块语法不一样导致的。

因此,我们解决这个问题只需要用下面这一种方法:

  1. 将上面的export from的语法稍加调整来适配TypeScript语法。具体改造如下:

    export {default as Session} from '_models/session'; //调整后不报错
    export * from '_models/read-item';// 之前不报错不需要调整
    Copy after login

总结

在做项目TypeScript改造的过程中,遇到了不少大大小小的坑。很多问题在网上都没有解决方案或者没有说明白具体的解决步骤,因此希望通过这一篇文章来帮助大家在进行TypeScript迁移时避免在我踩过的坑上再浪费时间。

相关推荐:

关于TypeScript在node项目中的实践分析

The above is the detailed content of TypeScrip reshaping problems and solutions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles