title: Extract type resolves type reference dependency using typescript compiler api
body: <p>I have two files. One is in named type.</p>
  <pre><code>export type ExternalType = {
    type: 'external'
  }
  </code></pre>
  <p>and another is</p>
  <pre><code>import { ExternalType } from './type'
      
  interface IA {
    (): number
  }
      
  interface IB {
    type: 'b'
  }
  
  interface IC extends IB {
    type: 'c'
  }
      
  type AliasedType = {
    type: 'aliased'
  }
    
  type Check = {
    type: 'a'
    funcInterface: IA
    normalInterface: IB
    inheritedInterface: IC
    external: ExternalType
    aliased: AliasedType
  }
      
  type DummyType = {
    id: string
    type: 'dummy'
  }
  </code></pre>
  <p>I want to extract Check type above specifying type name 'Check', and to get output the type form is followed by either.</p>
  <p>a) Dependency all type reference nodes are replaced resolved builtin typescript type.</p>
  <pre><code>type Check = {
    type: 'a'
    funcInterface: () =&gt; number // or expression similar to Function type
    normalInterface: {
      type: 'b'
    }
    inheritedInterface: {
      type: 'b'
      type: 'c'
    }
    external: {
      type: 'external'
    }
    aliased: {
      type: 'aliased'
    }
  }
  </code></pre>
  <p>or</p>
  <p>b) extract all depended types and integrate to one file</p>
  <pre><code>type ExternalType = {
    type: 'external'
  }
  
  interface IA {
    (): number
  }
      
  interface IB {
    type: 'b'
  }
  
  interface IC extends IB {
    type: 'c'
  }    
  
  type AliasedType = {
    type: 'aliased'
  }
      
  type Check = {
    type: 'a'
    funcInterface: IA
    normalInterface: IB
    inheritedInterface: IC
    external: ExternalType
    aliased: AliasedType
  }
      
  </code></pre>
  <p>The followed by partial code I wrote extracts Check type but, don't resolve dependency types.</p>
  <pre><code>function generateCodeFromAst(ast: ts.Node) {
    const resultFile = ts.createSourceFile(
      'dummy.ts',
      '',
      ts.ScriptTarget.Latest,
      /* setParentNodes */ false,
      ts.ScriptKind.TS,
    );
    const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
    const generatedCode = printer.printNode(
      ts.EmitHint.Unspecified,
      ast,
      resultFile,
    );
    return generatedCode;
  }
  
  // some omitted code 
  // visit 
        if (node.name.text === symbol) {
          const t = checker.getTypeAtLocation(node);
          // NOTE: https://stackoverflow.com/questions/67423762/typescript-compilerapi-how-to-get-expanded-type-ast
          const ast = checker.typeToTypeNode(
            t,
            undefined,
            ts.NodeBuilderFlags.NoTruncation | ts.NodeBuilderFlags.InTypeAlias,
          );
          console.log(generateCodeFromAst(ast!))
  </code></pre>
  <p>This outputs like</p>
  <pre><code>{ type: &quot;a&quot;; funcInterface: IA; normalInterface: IB; inheritedInterface: IC; external: ExternalType; aliased: AliasedType; }
  </code></pre>
  <p>Ultimately, I want to insert the Check type node AST to another AST with resolved type dependencies either way above. If we don't have dependencies type, the code works.</p>
  <pre><code>ts.factory.createTypeLiteralNode([
    ts.factory.createPropertySignature(
       undefined,
       ts.factory.createIdentifier('check'),
       undefined,
       CheckTypeAST,
    ),
  ]),
  </code></pre>
  <p>Is it possible?</p>