Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于目前Typescript规则中禁用namespace功能的一些想法 #58

Open
franckchen opened this issue Aug 12, 2021 · 2 comments
Open

Comments

@franckchen
Copy link
Contributor

根据目前的规则, Typescript的namespace是被推荐禁用的

'@typescript-eslint/no-namespace': 'error',

个人对于namespace钟爱有加, 下面试举一例

export class Person {
    gender: Person.gender;
}

export namespace Person {
    export type gender = 'male' | 'female';
}

可以看出将类型挂载在class上令整体代码更加面向对象,更加利于理解, 外部引用也更加自然

import {Person} from './Person';

const myGender: Person.gender = 'male';

另外Typescript Deep Dive中Enum with static functions也是很好的一个例子。感觉很赞

此外,使用使用namespace替换传统的export对象字面量,感觉也挺香的

故此,是否我们的规则应该放开对于namespace的使用呢?如果认可将PR进行一定调整~

@otakustay
Copy link
Member

我不建议开放namespace。模块化地管理类型更合适,如果类型多,可以import * as T from './some-module';这样子用

如果开放命名空间,滥用的可能性会大于正确、合适地使用。因此如果实在需要命名空间,用注释禁一下就好了

@franckchen
Copy link
Contributor Author

有道理, 目前非tsc自身,其他编译器,尤其是swc, 在面对namespace的时候都时常出点问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants