typescript - 选项参数中的 keyof
问题描述
在方法类型签名中,类型keyof this
可用于将参数约束为类的有效键的字符串名称。但是,如果该方法采用选项样式而不是位置参数,则它不起作用。IE:
class Foo {
// Allowed
m1(a: string, b: keyof this) {
...
}
// Error: A 'this' type is available only in a non-static member of a class or interface
m2(options: {a: string, b: keyof this}) {
...
}
}
有没有办法解决?谢谢。
解决方案
是的,您可以提取类型并作为泛型参数options
传递:this
type Options<T> = { a: string, b: keyof T };
class Foo {
m2(options: Options<this>) {}
}
推荐阅读
- ionic-framework - Ionic + Capacitor - iOS 上的 PWA 移动摄像头访问 - 在 Safari 中工作 - 但不是从主屏幕图标
- javascript - 类型错误:props.theTodos.map 不是函数
- php - laravel-excel 未定义索引:$
- python-3.x - 最大和子数组python
- magento2 - 需要在 magento 2 的结帐页面上编写自定义消息
- ansible - Ansible循环遍历具有相对路径的目录和子目录列表
- amazon-dynamodb - 如果我们使用映射器对象,我们可以使用带有表名覆盖的 DynamoDB 事务库吗?
- google-bigquery - 我们可以使用 Python 创建数据洞察报告吗?
- javascript - 在 HTML 文件中加密 HTTP Post 请求
- reactjs - 将 onChange 脚本添加到 React 中的输入框