有没有办法在打字稿中对具有可为空属性的数组进行排序?
Is there a way to sort an array with nullable properties in typescript?
这是我的代码:
export function getExpertTitle(
experiences: Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null;
}> | null
) {
const emptyExperience = {
title: null,
description: null,
endDate: null
};
let currentExperience = experiences && experiences[0];
// Here is were i start to have the error
// Object is possibly 'null' or 'undefined'.ts(2533)
for (let i = 0, l = experiences && experiences.length; i < (!isNullOrUndefined(l) && l); i++) {
if (isNullOrUndefined(experiences && experiences[i].endDate)) {
currentExperience = experiences && experiences[i];
} else if ((!isNullOrUndefined(currentExperience) && currentExperience.endDate.getTime()) > Date.now()) {
currentExperience = experiences && experiences[i];
}
}
所以,我想做的是比较 experiences 数组并使用 endDate 属性 按日期对它们进行排序,我想将 currentExperience 设置为最新体验。此外,如果 endDate 属性 是 'null' 体验仍在进行中,则默认为 currentExperience。
我原以为 for 循环看起来像这样,但我仍然收到错误消息:
for (let i = 0, l = experiences.length; i < l; i++) {
if (isNullOrUndefined(experiences[i].endDate)){
currentExperience = experiences && experiences[i];
break;
}
else if (experiences[i].endDate?.getTime() > currentExperience.endDate.getTime()) {
currentExperience = experiences && experiences[i];
}
}
我仍在努力解决这个问题,如果我找到解决方案,我会post。
同时,如有任何意见,我们将不胜感激。
出现此错误的原因是数组的类型定义表明任何条目都可能为空:
experiences: Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null;
}> | null // <------------------ HERE
要过滤掉 null
值,您可以使用
experiences.filter(x => x !== null) as Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null; }> // array cannot contain null, only objects with null properties
但是根据你的代码所做的,我认为你更喜欢在你的 for 循环中使用这样的东西:
for (let i = 0, l = experiences.length; i < l; i++) {
// below is now true if the object is null
if (isNullOrUndefined(experiences && experiences[i] && experiences[i].endDate)){
currentExperience = experiences && experiences[i];
break;
}
// ...
}
这是我的代码:
export function getExpertTitle(
experiences: Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null;
}> | null
) {
const emptyExperience = {
title: null,
description: null,
endDate: null
};
let currentExperience = experiences && experiences[0];
// Here is were i start to have the error
// Object is possibly 'null' or 'undefined'.ts(2533)
for (let i = 0, l = experiences && experiences.length; i < (!isNullOrUndefined(l) && l); i++) {
if (isNullOrUndefined(experiences && experiences[i].endDate)) {
currentExperience = experiences && experiences[i];
} else if ((!isNullOrUndefined(currentExperience) && currentExperience.endDate.getTime()) > Date.now()) {
currentExperience = experiences && experiences[i];
}
}
所以,我想做的是比较 experiences 数组并使用 endDate 属性 按日期对它们进行排序,我想将 currentExperience 设置为最新体验。此外,如果 endDate 属性 是 'null' 体验仍在进行中,则默认为 currentExperience。
我原以为 for 循环看起来像这样,但我仍然收到错误消息:
for (let i = 0, l = experiences.length; i < l; i++) {
if (isNullOrUndefined(experiences[i].endDate)){
currentExperience = experiences && experiences[i];
break;
}
else if (experiences[i].endDate?.getTime() > currentExperience.endDate.getTime()) {
currentExperience = experiences && experiences[i];
}
}
我仍在努力解决这个问题,如果我找到解决方案,我会post。 同时,如有任何意见,我们将不胜感激。
出现此错误的原因是数组的类型定义表明任何条目都可能为空:
experiences: Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null;
}> | null // <------------------ HERE
要过滤掉 null
值,您可以使用
experiences.filter(x => x !== null) as Array<{
title?: string | null;
description?: string | null;
endDate?: Date | null; }> // array cannot contain null, only objects with null properties
但是根据你的代码所做的,我认为你更喜欢在你的 for 循环中使用这样的东西:
for (let i = 0, l = experiences.length; i < l; i++) {
// below is now true if the object is null
if (isNullOrUndefined(experiences && experiences[i] && experiences[i].endDate)){
currentExperience = experiences && experiences[i];
break;
}
// ...
}