某个地方在项目中只有 一些像 这样的:
interface Result {
readonly items: readonly ResultItem[] | null;
}
interface ResultItem {
readonly Name : string;
readonly CreatedOn : string | undefined;
readonly Description: string;
}
或者这个(或者它们的变化):
type Result = {
items?: ResultItem[];
}
interface ResultItem {
Name : string;
CreatedOn? : string;
Description: string;
}
或者它可以是一个 type
而不是一个 interface
(只要确定你从来没有使用 class
来描述java,允许 object
数据不能 class
实例,因为该构造从来没有运行).
此外,你应该能用 camelCase
,不 PascalCase
为成员的性质。 所使用的名称喜欢 createdOn
而不是的 CreatedOn
在你所产生的手机中。
幸运的是,你不需要改变该类型/接口,只是改变你的稿以 安全 检查 .CreatedOn
和 那 Date.parse
没有回来 NaN
. 像这样:
- 的
result.items ?? []
部分是因为你的职位意味着 result.items
是可空的或者,也许-undefined
.
- 注意到当使用
map
有一个 =>
风格的功能,可能需要包对象的文字在 ()
所以JS引擎不会解释 {
和 }
作为框符。
const result: Result = ...
const currentDate = new Date();
const newResult = (result.items ?? []).filter( e => {
if( typeof e.CreatedOn === 'string' ) {
const parsed = Date.parse( e.CreatedOn );
if( !isNaN( parsed ) ) {
return ( currentDate - parsed ) > 90;
}
}
return false;
} );
虽然我个人想这样做与一个初始的 filter
和 map
步骤:
const items = result.items ?? [];
const currentDate = new Date();
const newResult = items
.filter( e => typeof e.CreatedOn === 'string' )
.map( e => ( { ...e, CreatedOn2: Date.parse( e.CreatedOn ) } ) )
.filter( e => !isNaN( e.CreatedOn2 ) )
.filter( e => ( currentDate - e.CreatedOn2 ) > 90 ) );
或者简化一步:
const items = result.items ?? [];
const currentDate = new Date();
const newResult = items
.filter( e => typeof e.CreatedOn === 'string' )
.map( e => Object.assign( e, { createdOn2: Date.parse( e.CreatedOn ) )
.filter( e => !isNaN( e.CreatedOn2 ) && ( currentDate - e.CreatedOn2 ) > 90 );
一个更好的解决方案:
如果你是在控制如何id产生然后你就可以确保某些(或所有)项目特性将始终被设定(并因此从来没有 undefined
或 null
),所以如果你能保证所有3个属性始终设置(永远不会 null
或 undefined
),然后更新类型/接口,以此:
interface ResultItem {
readonly name : string;
readonly createdOn : string;
readonly description: string;
}
- 注意到
camelCase
属性。
- 不可改变的数据 是一个巨大的好处,所以确保你的口的特性是所有
readonly
所有阵列 readonly T[]
和那个性都只注明 ?
或 | null
或 | undefined
作为适当的而不是简单的假设一种或另一种方式。
因此,请确保您使用 strictNullChecks
在你 tsconfig.json
或 tsc
选择! -其实,只要使用 strict
永远!
还考虑改变式也可与检,从使用一个 string
表示的一个日期(是否有任何gurantees约时?) 是一个本身可读Unix timestamp(在毫秒),这样就可以避免的问题 Date.parse
完全:
例如:
结果。cs:
public class ResultItem
{
[JsonProperty( "createdOn" )]
public DateTimeOffset CreatedOn { get; }
[JsonProperty( "createdOnUnix" )]
public Int64 CreatedOnUnix => this.CreatedOn.ToUnixTimeMilliseconds();
}
结果。ts:
interface ResultItem {
readonly createdOn : string;
readonly createdOnUnix: number;
}
const ninetyDaysAgo = new Date();
ninetyDaysAgo.setDate( ninetyDaysAgo.getDate() - 90 );
const newResult = items.filter( e => new Date( e.createdOnUnix ) < ninetyDaysAgo );
...这样的一个单一的线的工作。
上述可以作出即使是简单作为Unix timestamps只是整数,都直接可比性,所以 new Date()
可以避免内部 filter
是这样的:
const ninetyDaysAgo = new Date();
ninetyDaysAgo.setDate( ninetyDaysAgo.getDate() - 90 );
const ninetyDaysAgoUnix = ninetyDaysAgo.getTime();
const newResult = items.filter( e => e.createdOnUnix < ninetyDaysAgoUnix );
({ CreatedOn, ...item }) => ({
做什么呢? 我从来没有见过传播运营商...
用一个功能清单参数同时作为对象的文字。