Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
TypeScript Type Hoisting "chicken or egg" problem

Before someone marks this as a duplicate, I checked 2 other questions that didn't actually solve anything. They were only ideas for alternatives ways to do the exact problem, but didn't show a solution to my problem.

When making an array in TypeScript, I assigned the type any[] to it, since I haven't imported the type of the array yet. However, I have to pass this array to a require statement to make class methods work on this particular array.

If I make the array first, (using any[]) then I won't be able to type the array.

let blockchain: any[] = [];

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain);
type BlockType = InstanceType<typeof Block>;

// How do I set blockchain's type to BlockType[]?

However, if I import the classes first, I won't be able to pass the array to give the correct array to class methods

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain); // error
type BlockType = InstanceType<typeof Block>;

let blockchain: BlockType[] = [];

Importing the module twice, first with an empty array just to obtain the class, is not an option for me, since the original import's class methods will push data to the empty array and it'll be pointless.

EDIT: To be more specific about the module classes, this is what happens inside the other file.

function exp(blockchain: any[]): Object {
    class Block {
        // public x: number, etc...
        
        constructor(
            // stuff...
        }

        static generate(/* params */): Block {
            // do stuff, edit properties...

            let block = new Block(/* args */);
            blockchain.push(block); // Array that was passed to module
            return block;
        }
    }
    
    return {
        Block: Block
    }
}

Scotty Jamison's suggestion wouldn't work here because the Block class from import #1 wouldn't be the same as the one from import #2, and their methods would push to different arrays.

let {Block1} = require("./otherfile.js")([]);
let blockchain: Block1[] = [];
let {Block2} = require("./otherfile.js")(blockchain);

Block1.generate("yo");
Block2.generate("foobar");

console.log(blockchain.length); // 1
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!