问题
在使用vue3+vite+ts开发时,使用namespace时eslint提示如下错误:
|
1 |
ES2015 module syntax is preferred over custom TypeScript modules and namespaces.eslint@typescript-eslint/no-namespace |
解决方法
简单粗暴的解决方法,在.eslintrc中关闭该规则:
|
1 2 3 |
rules: { "@typescript-eslint/no-namespace": "off" } |
为什么
官方文档说明如下:
no-namespace
Disallow the use of custom TypeScript modules and namespaces.
Custom TypeScript modules (module foo {}) and namespaces (namespace foo {}) are considered outdated ways to organize TypeScript code. ES2015 module syntax is now preferred (import/export).
This rule still allows the use of TypeScript module declarations to describe external APIs (declare module 'foo' {}).
大体意思是:
no-namespace
禁止使用自定义Typescript模块和命名空间。
自定义Typescript模块(module foo {})和命名空间(namespace foo {})被认为是组织Typescript代码的过时方法。ES2015模块语法现在是首选(import/export)。
此规则仍然允许使用Typescript声明来描述外部API(declare module 'foo' {})。
因此,还是不要关闭该规则,在框架中使用import/export吧。
官方文档
https://typescript-eslint.io/rules/no-namespace/
https://typescript-eslint.io/rules/