简体   繁体   English

如何配置TypeScript对不正确的导入语句抛出错误/警告?

[英]How to configure TypeScript to throw an error/warning for incorrect import statement?

I am working on converting a large application from JavaScript (Backbone and Angular 1) to TypeScript. 我正在将大型应用程序从JavaScript(主干和Angular 1)转换为TypeScript。 When we convert a file that is used by other files we understand that we have to update the import statements in those other JavaScript files so that it imports the new TypeScript file correctly. 当我们转换其他文件使用的文件时,我们了解到我们必须更新那些其他JavaScript文件中的import语句,以便它正确导入新的TypeScript文件。 Our syntax update in fake-file.js is as follows. 我们在fake-file.js中的语法更新如下。

Before: import OurService from 'our.service'; 之前: import OurService from 'our.service';

After: import { OurService } from 'our.service'; 之后: import { OurService } from 'our.service';

I understand that this is an easy change but TypeScript is new to many developers and there have been problems with people missing some of these import statements or forgetting to change them all together resulting in some issues during runtime. 我知道这是一个容易的更改,但是TypeScript对于许多开发人员来说是新手,而且人们会遗漏某些导入语句或忘记将它们全部更改在一起而导致运行时出现问题的问题。 I have looked into compiler options but I do not see any that would fix this issue but I could be misinterpreting them. 我已经研究了编译器选项,但是看不到能解决此问题的任何选项,但是我可能会误解它们。

Question: Is there a way to configure the compiler (or a Visual Studio Code plugin) to throw a warning or an error to prevent this from happening? 问题:有没有一种方法可以配置编译器(或Visual Studio Code插件)以发出警告或错误以防止这种情况发生?

I assume that I understood your requirement and possibly you need to adapt a linting process and consequently I would suggest the following tools (which I also use in my project): 我假设我了解您的要求,可能您需要调整起毛过程,因此,我建议使用以下工具(我也在项目中使用过):

  1. Airbnb Javascript style guide (your import statement concern- https://github.com/airbnb/javascript#modules ). Airbnb Javascript样式指南(您的导入声明关注-https ://github.com/airbnb/javascript#modules )。 These are a well-defined set of standards defined for any JS application (including ES). 这些是为任何JS应用程序(包括ES)定义的一组定义明确的标准。
  2. ESLint . ESLint You can run ESLint from the terminal and configure it for your project that highlights warning/errors in your code. 您可以从终端运行ESLint,并为您的项目配置ESLint,以突出显示代码中的警告/错误。 If this looks complicated, you can generate the tslint document for your entire project in the website itself. 如果这看起来很复杂,则可以在网站本身中为整个项目生成tslint文档。 Click on rules configuration and configure the ES rules for your project. 单击rules configuration然后为您的项目配置ES规则。 There are some import related rules too. 也有一些与import相关的规则。 PS: Feel free to add your comments. PS:随时添加您的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何忽略打字稿中的导入语句 - How to ignore import statement in Typescript 如何使用try catch语句抛出错误? - How to throw an error using try catch statement? 表达打字稿错误抛出“任何”警告 - express typescript err throw 'any' warning typescript 中的导入语句 - import statement in typescript 如何在Typescript中将import语句与import all语句组合? - How to combine import statements with an import all statement in Typescript? 如何将TypeScript导入语句转换为JS Require语句 - How to convert a TypeScript import statement into a JS Require statement Angular TS1003 标识符预期在 throw import 语句中,我的错误源于从 'rxjs' 导入 throw - Angular TS1003 Identifier expected on throw import statement, my error stems from the import of throw from 'rxjs' 如果在我的 catch 块中引发错误的代码被 if 语句包围,为什么 Typescript 会引发错误? - Why does Typescript throw an error if the code that throws an error in my catch block is surrounded by an if statement? 出现typeScript错误“模块,类,接口,枚举,导入或声明” - typeScript error 'module, class, interface, enum, import or statement' expected 当导出文件中未定义导出属性时,import语句不会引发任何错误 - import statement doesn't throw any error when a exported property is not defined in the export file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM