title: How to define a type and make sure it's part of another type in typescript?
body: <p>I have a big type:</p>
  
  <pre><code>type BigType = {
    aaa: string,
    bbb?: number,
    ccc: boolean[],
    extra?: {
      [key in string]?: string
    },
    nested1: {
      nested2: {
        nested3: {
          [key in string]?: string
        }
      }
    }
  }
  </code></pre>
  
  <p>And I want to define another type and make sure it's subset of the <code>BigType</code>, so I defined a <code>RecursivePartial</code> type:</p>
  
  <pre><code>type RecursivePartial&lt;T&gt; = {
    [P in keyof T]?:
    T[P] extends (infer U)[] ? RecursivePartial&lt;U&gt;[] :
      T[P] extends object ? RecursivePartial&lt;T[P]&gt; :
        T[P];
  };
  </code></pre>
  
  <p>and </p>
  
  <pre><code>type PartOf&lt;T, X extends RecursivePartial&lt;T&gt;&gt; = X;
  </code></pre>
  
  <p>Now I can define a small type which is only part of the BigType:</p>
  
  <pre><code>type SmallType = PartOf&lt;BigType, {
    aaa: string;
    extra: { ddd: string };
    nested1: { nested2: {} }
  }&gt;
  </code></pre>
  
  <p>The problem is I can add properties which is not part of <code>BigType</code> too:</p>
  
  <pre><code>type SmallType = PartOf&lt;BigType, {
    aaa: string;
    extra: { ddd: string };
    nested1: { nested2: {} },
  
    someOtherProperty1: string, // not part of BigType
    someOtherProperty2: string, // not part of BigType
  }&gt;
  </code></pre>
  
  <p>How to fix it?</p>