Javascript 从可选到对象
我定义了这个类型Javascript 从可选到对象,javascript,node.js,typescript,Javascript,Node.js,Typescript,我定义了这个类型 export type Optional<T> = T | null | undefined; 但我有一个错误: Argument of type 'Optional<Hostel>' is not assignable to parameter of type 'Hostel'. 类型为“Optional”的参数不能分配给类型为“Hostel”的参数。 我不知道如何从可选转换到hotel执行if条件来检查notnull,然后分配您可能需要检查,
export type Optional<T> = T | null | undefined;
但我有一个错误:
Argument of type 'Optional<Hostel>' is not assignable to parameter of type 'Hostel'.
类型为“Optional”的参数不能分配给类型为“Hostel”的参数。
我不知道如何从
可选转换到hotel
执行if
条件来检查notnull,然后分配您可能需要检查,比如if(id){wait this.repository.getHostelAsync(id);}否则{/*如果id为null,那么怎么办,由您决定*/}
执行一个if
条件来检查notnull,然后分配您可能需要检查的内容,比如if(id){等待这个.repository.getHostelAsync(id);}否则{/*如果id为null,那么怎么办,取决于您*/}
await this.repository.getHostelAsync(id);
Argument of type 'Optional<Hostel>' is not assignable to parameter of type 'Hostel'.