繁体   English   中英

如何实现 TypeScript 深度部分映射类型不破坏数组属性

[英]How to implement TypeScript deep partial mapped type not breaking array properties

关于如何递归地将 TypeScript 的 Partial 映射类型应用于接口,同时不破坏任何具有数组返回类型的键的任何想法?

以下方法还不够:

interface User {  
  emailAddress: string;  
  verification: {
    verified: boolean;
    verificationCode: string;
  }
  activeApps: string[];
}

type PartialUser = Partial<User>; // does not affect properties of verification  

type PartialUser2 = DeepPartial<User>; // breaks activeApps' array return type;

export type DeepPartial<T> = {
  [ P in keyof T ]?: DeepPartial<T[ P ]>;
}

有任何想法吗?

更新:已接受的答案 - 现在是一个更好、更通用的解决方案。

找到了一种临时解决方法,其中涉及类型和两个映射类型的交集,如下所示。 最显着的缺点是您必须提供属性覆盖来恢复被污染的键,即具有数组返回类型的键。

例如

type PartialDeep<T> = {
  [ P in keyof T ]?: PartialDeep<T[ P ]>;
}
type PartialRestoreArrays<K> = {
  [ P in keyof K ]?: K[ P ];
}

export type DeepPartial<T, K> = PartialDeep<T> & PartialRestoreArrays<K>;

interface User {  
 emailAddress: string;  
 verification: {
   verified: boolean;
   verificationCode: string;
 }
 activeApps: string[];
}

export type AddDetailsPartialed = DeepPartial<User, {
 activeApps?: string[];
}>

像这样

关于如何将 TypeScript 的 Partial 映射类型递归地应用于接口的任何想法,同时不破坏具有数组返回类型的任何键?

以下方法还不够:

interface User {  
  emailAddress: string;  
  verification: {
    verified: boolean;
    verificationCode: string;
  }
  activeApps: string[];
}

type PartialUser = Partial<User>; // does not affect properties of verification  

type PartialUser2 = DeepPartial<User>; // breaks activeApps' array return type;

export type DeepPartial<T> = {
  [ P in keyof T ]?: DeepPartial<T[ P ]>;
}

有任何想法吗?

更新:接受的答案 - 现在更好,更通用的解决方案。

找到了一个临时解决方法,它涉及类型和两个映射类型的交集,如下所示。 最显着的缺点是您必须提供属性覆盖来恢复被污染的键,即具有数组返回类型的键。

例如

type PartialDeep<T> = {
  [ P in keyof T ]?: PartialDeep<T[ P ]>;
}
type PartialRestoreArrays<K> = {
  [ P in keyof K ]?: K[ P ];
}

export type DeepPartial<T, K> = PartialDeep<T> & PartialRestoreArrays<K>;

interface User {  
 emailAddress: string;  
 verification: {
   verified: boolean;
   verificationCode: string;
 }
 activeApps: string[];
}

export type AddDetailsPartialed = DeepPartial<User, {
 activeApps?: string[];
}>

像这样

关于如何将 TypeScript 的 Partial 映射类型递归地应用于接口的任何想法,同时不破坏具有数组返回类型的任何键?

以下方法还不够:

interface User {  
  emailAddress: string;  
  verification: {
    verified: boolean;
    verificationCode: string;
  }
  activeApps: string[];
}

type PartialUser = Partial<User>; // does not affect properties of verification  

type PartialUser2 = DeepPartial<User>; // breaks activeApps' array return type;

export type DeepPartial<T> = {
  [ P in keyof T ]?: DeepPartial<T[ P ]>;
}

有任何想法吗?

更新:接受的答案 - 现在更好,更通用的解决方案。

找到了一个临时解决方法,它涉及类型和两个映射类型的交集,如下所示。 最显着的缺点是您必须提供属性覆盖来恢复被污染的键,即具有数组返回类型的键。

例如

type PartialDeep<T> = {
  [ P in keyof T ]?: PartialDeep<T[ P ]>;
}
type PartialRestoreArrays<K> = {
  [ P in keyof K ]?: K[ P ];
}

export type DeepPartial<T, K> = PartialDeep<T> & PartialRestoreArrays<K>;

interface User {  
 emailAddress: string;  
 verification: {
   verified: boolean;
   verificationCode: string;
 }
 activeApps: string[];
}

export type AddDetailsPartialed = DeepPartial<User, {
 activeApps?: string[];
}>

像这样

关于如何将 TypeScript 的 Partial 映射类型递归地应用于接口的任何想法,同时不破坏具有数组返回类型的任何键?

以下方法还不够:

interface User {  
  emailAddress: string;  
  verification: {
    verified: boolean;
    verificationCode: string;
  }
  activeApps: string[];
}

type PartialUser = Partial<User>; // does not affect properties of verification  

type PartialUser2 = DeepPartial<User>; // breaks activeApps' array return type;

export type DeepPartial<T> = {
  [ P in keyof T ]?: DeepPartial<T[ P ]>;
}

有任何想法吗?

更新:接受的答案 - 现在更好,更通用的解决方案。

找到了一个临时解决方法,它涉及类型和两个映射类型的交集,如下所示。 最显着的缺点是您必须提供属性覆盖来恢复被污染的键,即具有数组返回类型的键。

例如

type PartialDeep<T> = {
  [ P in keyof T ]?: PartialDeep<T[ P ]>;
}
type PartialRestoreArrays<K> = {
  [ P in keyof K ]?: K[ P ];
}

export type DeepPartial<T, K> = PartialDeep<T> & PartialRestoreArrays<K>;

interface User {  
 emailAddress: string;  
 verification: {
   verified: boolean;
   verificationCode: string;
 }
 activeApps: string[];
}

export type AddDetailsPartialed = DeepPartial<User, {
 activeApps?: string[];
}>

像这样

关于如何将 TypeScript 的 Partial 映射类型递归地应用于接口的任何想法,同时不破坏具有数组返回类型的任何键?

以下方法还不够:

interface User {  
  emailAddress: string;  
  verification: {
    verified: boolean;
    verificationCode: string;
  }
  activeApps: string[];
}

type PartialUser = Partial<User>; // does not affect properties of verification  

type PartialUser2 = DeepPartial<User>; // breaks activeApps' array return type;

export type DeepPartial<T> = {
  [ P in keyof T ]?: DeepPartial<T[ P ]>;
}

有任何想法吗?

更新:接受的答案 - 现在更好,更通用的解决方案。

找到了一个临时解决方法,它涉及类型和两个映射类型的交集,如下所示。 最显着的缺点是您必须提供属性覆盖来恢复被污染的键,即具有数组返回类型的键。

例如

type PartialDeep<T> = {
  [ P in keyof T ]?: PartialDeep<T[ P ]>;
}
type PartialRestoreArrays<K> = {
  [ P in keyof K ]?: K[ P ];
}

export type DeepPartial<T, K> = PartialDeep<T> & PartialRestoreArrays<K>;

interface User {  
 emailAddress: string;  
 verification: {
   verified: boolean;
   verificationCode: string;
 }
 activeApps: string[];
}

export type AddDetailsPartialed = DeepPartial<User, {
 activeApps?: string[];
}>

像这样

这似乎工作正常。

type DeepOptional<T> = 
  T extends Date | Function ? T 
  : T extends (infer R)[] ? DeepOptional<R>[]
  : T extends Record<PropertyKey, any> ? 
    {
      [K in keyof T]?: DeepOptional<T[K]>
    } 
  : T;

操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM