title: Using a user-defined object as types in TypeScript
body: <p>I am in this scenario where I don't know what a type will look like, whether it is undefined or a boolean, string or a number, etc. I am working on a library that will parse some string content and return an object of properties that the user has described using "type objects".</p>
  
  <p>The <code>Type</code> is an interface for how the type objects should look like, and it goes like this:</p>
  
  <pre><code>/**
   * A type object.
   */
  export default interface IType&lt;O&gt;
  {
      /** The name of the type. */
      name: string;
  
      /** A description of the type. */
      description?: string;
  
      /**
       * Parse the given input content and return the parsed output value.
       * @param input The given input value.
       * @returns The parsed output.
       */
      parse(input: string): O;
  }
  </code></pre>
  
  <p>I have provided the user with 3 pre-defined "type objects" that follow this interface (Number as numberType, Boolean as booleanType and String as stringType). The user can use these types to gather arguments from a string.</p>
  
  <p>The argument interface defines how an argument should be specified in code.</p>
  
  <pre><code>/**
   * An object describing how an argument will look like.
   */
  export default interface IArg&lt;T&gt;
  {
      /**
       * The name of the argument, this is how you use the argument later
       * in the command object.
       */
      name: string;
  
      /** A description about the argument. */
      description?: string;
  
      /** The type the argument should be resolved with. */
      type: IType&lt;T&gt;;
  
      /** Should this take all the parameters left of the string? */
      rest?: boolean;
  
      /** Is the argument required? */
      required?: boolean;
  }
  </code></pre>
  
  <p>The user creates a command class and passes some arguments to the constructor including an array of arguments;</p>
  
  <pre><code>[
      {
          name: "age"
          type: numberType
      },
      {
          name: "name",
          type: stringType
      }
  ]
  </code></pre>
  
  <p>When the command string is parsed these arguments will be added into an object <code>input.args</code>. Somehow I want the typescript parser to know what may or may not be provided by the information given by the user.</p>
  
  <p>E.g. if an argument is required and is a type of <code>IArg&lt;string&gt;</code> (let's call it <code>name</code>) then the parser knows that <code>input.args.name</code> is definetly a string, but if it isn't required, it might be undefined or a string.</p>
  
  <p>Is this possible to achieve?</p>
  
  <blockquote>
    <p><strong>Edit</strong>: I found <a href="https://stackoverflow.com/a/57829842/108084471">this</a> answer by <a href="https://stackoverflow.com/users/2887218/jcalz">jcalz</a> which takes an object of key-value pairs which does exactly what I'm looking for; but I'm unsure on how to implement it with arrays.</p>
  </blockquote>