Преглед на файлове

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: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.
  */