为什么 partialType 不使属性可为空?

Why does partialType not make properties nullable?

有谁知道为什么 partialType 不使属性可为空?它将 @IsOptional 添加到也允许 null 作为有效值的属性,但是从 partialType 返回的类型仅 returns T | undefined 而不是 T | undefined | null ,这会导致 TypeScript 的严格空值检查模式出现问题。我打开了an issue一下;投稿人说这是预期的行为,但我不明白是怎么回事。

可以通过用 NullableType

包装 partialType 来修复
type Nullable<T> = {
  [P in keyof T]: T[P] | null;
};

export function NullableType<T>(classRef: Type<T>): Type<Nullable<T>> {
  return classRef as Type<Nullable<T>>;
}