瀏覽代碼

feat(common): Add DeepRequired type

Michael Bromley 6 年之前
父節點
當前提交
c77e36548e
共有 1 個文件被更改,包括 14 次插入0 次删除
  1. 14 0
      packages/common/src/shared-types.ts

+ 14 - 0
packages/common/src/shared-types.ts

@@ -13,6 +13,20 @@ export type DeepPartial<T> = {
 };
 };
 // tslint:enable:no-shadowed-variable
 // tslint:enable:no-shadowed-variable
 
 
+// tslint:disable:ban-types
+/**
+ * A recursive implementation of Required<T>.
+ * Source: https://github.com/microsoft/TypeScript/issues/15012#issuecomment-365453623
+ */
+export type DeepRequired<T, U extends object | undefined = undefined> = T extends object
+    ? {
+          [P in keyof T]-?: NonNullable<T[P]> extends NonNullable<U | Function | Type<any>>
+              ? NonNullable<T[P]>
+              : DeepRequired<NonNullable<T[P]>, U>
+      }
+    : T;
+// tslint:enable:ban-types
+
 /**
 /**
  * A type representing the type rather than instance of a class.
  * A type representing the type rather than instance of a class.
  */
  */