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系列:为 Reflect.construct 增加类型检查 #31

Open
e2tox opened this issue Sep 11, 2020 · 0 comments
Open

巧用TypeScript系列:为 Reflect.construct 增加类型检查 #31

e2tox opened this issue Sep 11, 2020 · 0 comments

Comments

@e2tox
Copy link
Owner

e2tox commented Sep 11, 2020

起因

由于原生的Reflect.construct并没有类型检查,所以在日常使用中,一不小心,参数写错一个,都不会给出错误提示,项目后期如果因此引发问题,那么会浪费不少时间来定位和修复。

如何增加类型检查?

既然系统没有类型检查,那只能动手自己做一个了。把以下代码粘贴到你的TypeScript项目中就可以了:

// 定义一个基类
interface AbstractConstructor<T> extends Function {
  readonly prototype: T;
}

// 定义参数
type AgentParameters<T> = T extends new (...args: infer P) => any ? P : Arguments;

// 定义返回值
type Agent<T> = T extends AbstractConstructor<infer R> ? R : never;

// 将 Reflect.construct 包装一下,大功告成
class Domain {
  static construct<T extends Function>(target: T, params: AgentParameters<T>): Agent<T> {
    return Reflect.construct(target, params);
  }
}

例子

// 带有构造参数的强类型
class MyClass {
   constructor(public name: string) {
   }
}

// 将
Reflect.construct(Class, ["e2tox"])

// 替换为
Domain.construct(MyClass, ["e2tox"]);

// 这样的话使用 number 作为参数,就会被提示出错
Domain.construct(MyClass, [123]);

写更好的程序,做更好的自己。祝大家编程愉快!

@e2tox e2tox changed the title 错误原来是因为 Reflect.construct 没有类型检查! 巧用TypeScript系列:为 Reflect.construct 增加类型检查 Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant