我试图在对象中引入查找类型。假设我的对象看起来像
class PersonList {
persons = {
john: 'description of john',
bob: 'description of bob'
}
}
Run Code Online (Sandbox Code Playgroud)
我想要一个吸气剂来获取一个人,persons但不指定对象persons。
来自getProperty文档
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key]; // Inferred type is T[K]
}
Run Code Online (Sandbox Code Playgroud)
想要有一个obj我想在吸气剂中摆脱的东西。我尝试过代理 getter,但没有成功:
class PersonList {
persons = {
john: 'description of john',
bob: 'description of bob'
};
getPerson(name) {
return this.getProperty(this.persons, name);
}
private getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key]; // Inferred type is T[K]
}
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,当尝试执行类似操作时,这不会引发错误personList.getPerson('someonenotincluded');- 而且自动完成功能也不起作用。
我会采用该内联类型并为其命名(但请继续阅读,您不必这样做):
interface Persons {
john: string;
bob: string;
}
Run Code Online (Sandbox Code Playgroud)
然后您可以将其用作keyof Persons参数类型getPerson:
class PersonList {
persons: Persons = {
john: 'description of john',
bob: 'description of bob'
};
getPerson(name: keyof Persons) {
return this.persons[name];
}
}
Run Code Online (Sandbox Code Playgroud)
那么如果pl是一个PersonList:
console.log(pl.getPerson('john')); // Works
console.log(pl.getPerson('someonenotincluded')); // Error
Run Code Online (Sandbox Code Playgroud)
但是,如果您希望将其保持内联,则可以使用keyof PersonList['persons']作为参数类型:
class PersonList {
persons = {
john: 'description of john',
bob: 'description of bob'
};
getPerson(name: keyof PersonList['persons']) {
return this.persons[name];
}
}
Run Code Online (Sandbox Code Playgroud)
在您提出的评论中:
是否可以在抽象类中实现它?...在抽象类中实现 getter 会很棒,但到目前为止我还没有找到解决方案。
...带有此代码模板的链接:
abstract class AbstractPersonList {
protected abstract persons: { [name: string]: string };
}
class Persons extends AbstractPersonList {
persons = {
john: 'this is john',
}
}
class MorePersons extends AbstractPersonList {
persons = {
bob: 'this is bob',
}
}
Run Code Online (Sandbox Code Playgroud)
您可以参数化AbstractPersonList:
abstract class AbstractPersonList<T extends {[name: string]: string}> {
// ------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
protected abstract persons: T;
public getPerson(name: keyof T): string {
return this.persons[name];
}
}
Run Code Online (Sandbox Code Playgroud)
那么你就会有:
class Persons extends AbstractPersonList<{john: string}> {
// -------------------------------------^^^^^^^^^^^^^^^^
persons = {
john: 'this is john',
}
}
class MorePersons extends AbstractPersonList<{bob: string}> {
// -----------------------------------------^^^^^^^^^^^^^^^
persons = {
bob: 'this is bob',
}
}
Run Code Online (Sandbox Code Playgroud)
这导致了这些结果,我认为这就是您正在寻找的结果:
let n = Math.random() < 0.5 ? 'john' : 'bob';
const p = new Persons();
console.log(p.getPerson('john')); // Works
console.log(p.getPerson('bob')); // FAILS: Argument of type '"bob"' is not assignable to parameter of type '"john"'.
console.log(p.getPerson(n)); // FAILS: Argument of type 'string' is not assignable to parameter of type '"john"'.
const mp = new MorePersons();
console.log(mp.getPerson('john')); // FAILS: Argument of type '"john"' is not assignable to parameter of type '"bob"'.
console.log(mp.getPerson('bob')); // Works
console.log(mp.getPerson(n)); // FAILS: Argument of type 'string' is not assignable to parameter of type '"bob"'.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7876 次 |
| 最近记录: |