title: Typescript how to constrain function parameter with the properties of an interface
body: <p>Hi guys I'm trying to find a way to constrain a function parameter, so it only takes "strings" constrained to an interface properties as I did in function validate fields:</p>
  
  <p><strong>Note:</strong> This is just the definition typescript code to make the question simpler.</p>
  
  <p><strong>index.d.ts</strong></p>
  
  <pre><code>export interface FieldErrors {
    errors: Array&lt;FieldError&gt;;
  }
  
  export type FieldsErrors&lt;TForm&gt; = {
    [k in keyof TForm]: FieldErrors;
  }
  
  export interface ValidateFieldsCallback&lt;TForm&gt; { (fieldsErrors: FieldsErrors&lt;TForm&gt;, values: TForm): void; };
  
  export interface FormShape&lt;TForm&gt; {
    getFieldValue(fieldName: 'documentNumber' | 'userName'): void; // HOW TO FIX THIS
    validateFields(callback: ValidateFieldsCallback&lt;TForm&gt;): void;
    validateFields(fieldsNames: Array&lt;string&gt;, callback: ValidateFieldsCallback&lt;TForm&gt;): void;
  }
  </code></pre>
  
  <p><strong>example.ts</strong></p>
  
  <pre><code>interface SignupForm {
      documentNumber: number;
      userName: string;
  }
  
  const testForm = &lt;FormShape&lt;SignupForm&gt;&gt;{};
  
  testForm.validateFields((values) =&gt; {
      console.log(values.documentNumber); // OK
      console.log(values.userName); // OK
      console.log(values.other); // ERROR
  });
  
  // THIS SHOULD BE FIXED
  const documentNumber = testForm.getFieldValue('documentNumber');
  </code></pre>
  
  <p>As you can see, I'm able to constrain the parameter <strong>fieldsErrors</strong> type of the <strong>validateFields</strong> callback, but I need to fix the function <strong>getFieldValue</strong> to only accept a "correct" field name based on the interface properties, and also return the correct type based on the interface type nor void.</p>
  
  <p>Any help would be very appreciated.</p>