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

类属性装饰器需要初始值 #4

Open
noahziheng opened this issue Mar 18, 2022 · 6 comments
Open

类属性装饰器需要初始值 #4

noahziheng opened this issue Mar 18, 2022 · 6 comments
Assignees

Comments

@noahziheng
Copy link
Member

如这部分代码所示,Inject 在类属性上生效是会被默认的 TS 规则视为未初始化,考虑处理:

  • 全局加规则 ignore 掉
  • 如同 nest 的最终处理,Inject 作为参数装饰器用在 constructor 里
  • other idea?

https://github.com/artusjs/core/pull/10/files/79a727d28b9546ec2d3fba638c1d0b08b746f493#diff-16a8f18a1e6d24ab6125db9b965edc8d7970d184e44ada35f3ccbf71a21670a4R13

@hyj1991
Copy link
Member

hyj1991 commented Mar 18, 2022

现在这样的写法是比较合理的,放到 constructor 里指的是什么样的形式?

ignore 的问题可以通过修改规则配置来解决,毕竟 lint 本身的目的是为了防止代码出现静态检查可以发现的错误,如果此处明确是使用 @inject 赋值初始化的话,本来就是没有问题的。

@JerrysShan
Copy link
Collaborator

但是不能完全约束用户用构造函数初始化属性,改变配置也可以

@noahziheng
Copy link
Member Author

@hyj1991 类似这种形式,nest 和 angular 没少用

import { Injectable, Inject } from '@nestjs/common';

@Injectable()
export class HttpService<T> {
  constructor(@Inject('HTTP_OPTIONS') private httpClient: T) {}
}

不过确实不能强迫用户这么用,太拉了

@noahziheng
Copy link
Member Author

改 ignore 配置的话,用户工程的 tsconfig 也需要这么配,得再看看友商都是怎么玩的,有没更优解法

@hyj1991
Copy link
Member

hyj1991 commented Mar 18, 2022

改 ignore 配置的话,用户工程的 tsconfig 也需要这么配,得再看看友商都是怎么玩的,有没更优解法

或者初始化的时候 extend 我们一个公共的 tsconfig?后面应该会提供工具生成脚手架都默认类似这样的模板:

{
  "extends": "@artus/tsconfig",
  "compilerOptions": {

  }
}

@kurten
Copy link

kurten commented Aug 10, 2022

其实可以参考spring的做法,有 Value 装饰器

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

4 participants