Typescript 类型脚本类型a或类型b
我有以下两个接口Typescript 类型脚本类型a或类型b,typescript,Typescript,我有以下两个接口 interface IComment extends IData { comment: string; } interface IHistory extends IData{ differences: any[]; timeStamp: number; } 它们都延伸了 interface IData { user: string; date: Moment | string; isHistory: boolean; } 现在来谈谈问题 我有一个包含
interface IComment extends IData {
comment: string;
}
interface IHistory extends IData{
differences: any[];
timeStamp: number;
}
它们都延伸了
interface IData {
user: string;
date: Moment | string;
isHistory: boolean;
}
现在来谈谈问题
我有一个包含IComment和IHistory元素的数组
const data: Array<IHistory | IComment> = [...someHistoryArray, ...someCommentArray]
嗯,我找到了两个对我来说不够满意的解决方案
(entry as IHistory).timeStamp
const historyEntry: IHistory = entry as IHistory;
还有其他可能的解决方案吗?如果在每个界面中添加特定的定义,您可以使用
isHistory
作为工会的判别标准:
interface IComment extends IData {
comment: string;
isHistory: false;
}
interface IHistory extends IData {
differences: any[];
timeStamp: number;
isHistory: true;
}
interface IData {
user: string;
date: string;
isHistory: boolean;
}
let data: Array<IComment | IHistory>=[]
data.map((entry: IHistory | IComment) => {
if(entry.isHistory === true) {
entry.timeStamp //ok
} else {
entry.comment //ok
}
})
接口图标扩展IData{
注释:字符串;
故事:假的;
}
接口IHistory扩展IData{
差异:任何[];
时间戳:数字;
故事:真实;
}
界面IData{
用户:字符串;
日期:字符串;
故事:布尔;
}
let data:Array=[]
data.map((条目:IHistory | IComment)=>{
if(entry.isHistory==true){
entry.timeStamp//ok
}否则{
entry.comment//ok
}
})
另一种可能是使用用户定义的类型保护,即帮助编译器推导参数是否具有特定类型的函数。下面的代码应该可以解决您的特定问题-我在更改的位中添加了注释
interface IComment extends IData {
comment: string;
}
interface IHistory extends IData {
differences: any[];
timeStamp: number;
}
interface IData {
user: string;
date: Moment | string;
isHistory: boolean;
}
const data: Array<IHistory | IComment> = [];
data.map((entry: IHistory | IComment) => {
// Explicitely narrows down the type to IHistory within the block
if (isHistory(entry)) {
// entry.timeStamp
} else {
// entry.comment
}
});
// User-Defined Type Guard
function isHistory(data: IData): data is IHistory {
return data.isHistory;
}
接口图标扩展IData{
注释:字符串;
}
接口IHistory扩展IData{
差异:任何[];
时间戳:数字;
}
界面IData{
用户:字符串;
日期:力矩|字符串;
故事:布尔;
}
常量数据:数组=[];
data.map((条目:IHistory | IComment)=>{
//明确地将类型缩小到块内的IHistory
如果(输入){
//entry.timeStamp
}否则{
//entry.comment
}
});
//用户定义的类型保护
函数isHistory(数据:IData):数据是IHistory{
返回data.story;
}
有关更多信息,请参见用户定义的类型保护。
if(entry.isHistory)
应为enough@bambam你是说在界面上?不,它们是布尔文字类型。@ LoMe你会想,但是Type Script不认为其他的只是用“<代码>”来保护,如果(条目。历史),那么条目类型不会因为这个检查而缩小。(我总是在发帖前尝试,if(entry.ishitory)
将与strictNullChecks一起工作,但我尝试提供可以正常工作的代码)是的,在界面中-在那里找到您的是正确的…-)@TitianCernicova Dragomir true,我的意思是,如果(entry.ishitory==true),if(entry.ishitory)
就足够了。通过执行if(entry.isHistory==true)
,您只需说if(true==true)
,而您只需说if(true)
interface IComment extends IData {
comment: string;
}
interface IHistory extends IData {
differences: any[];
timeStamp: number;
}
interface IData {
user: string;
date: Moment | string;
isHistory: boolean;
}
const data: Array<IHistory | IComment> = [];
data.map((entry: IHistory | IComment) => {
// Explicitely narrows down the type to IHistory within the block
if (isHistory(entry)) {
// entry.timeStamp
} else {
// entry.comment
}
});
// User-Defined Type Guard
function isHistory(data: IData): data is IHistory {
return data.isHistory;
}