简体   繁体   English

接口属性属于不同接口属性的类型

[英]Interface property be of the type of a different interface property

So I am trying to coordinate two separate interfaces in an instance of a table, a table which I would like to make filtering optional so I started with two interfaces for this (leaving out the rest of the implementation). 所以我试图在一个表的实例中协调两个单独的接口,一个表我想使过滤可选,所以我开始使用两个接口(省略了其余的实现)。 One to hold data about the columns and one to hold the data of the filtering 一个用于保存有关列的数据,另一个用于保存过滤数据

interface Column {
  headerTitle: string;
  headerId: string;
}

interface Filter {
  filteredHeaderIds: string []; // I would like this to be of type of headerId
  noDataMessage: string;
}

In the implementation above, I would want filterHeaderIds to be tied to whatever the user implements as a part of the Column . 在上面的实现中,我希望filterHeaderIds绑定到用户实现的任何Column作为Column的一部分。
Example: 例:

const columns: Column[] = [{
  headerTitle: 'Person Name',
  headerId: 'name',
}, {
  headerTitle: 'Person Age',
  headerId: 'age',
}];

const filterData: Filter = {
  filteredHeaderIds: ['name','throwError'], 
  noDataMessage: 'No Data Found',
}

Since throwError isn't a defined headerId I would like to see if it's possible to have typescript throw an error. 由于throwError不是已定义的headerId我想看看是否有可能让typescript抛出错误。 Is something like this possible? 这样的事情可能吗?

Thanks! 谢谢!

The closest thing I can think of is string literal types . 我能想到的最接近的是字符串文字类型 This would only be a valid solution if you are saying that your column definitions would be part of compiled code: 如果您说您的列定义将是已编译代码的一部分,那么这只是一个有效的解决方案:

type ColumnHeaderId = 'name' | 'age';

interface Column {
  headerTitle: string;
  headerId: ColumnHeaderId;
}

interface Filter {
  filteredHeaderIds: ColumnHeaderId[];
  noDataMessage: string;
}

However, if you are wanting the columns to be completely dynamically generated, I think you'll be stuck with implementing run-time logical checking for ID fields matching. 但是,如果您希望完全动态生成列,我认为您将无法实现ID字段匹配的运行时逻辑检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM