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

211
Views
Typescript static method does not exist on type someclass

I have code like this -

type StateTypes = State1 | State2;
    
class State1 { 
    static handleA (): StateTypes { 
        // Do Something
        return State2; 
    }
    static handleB (): StateTypes {
        // Do Something
        return State1;
    }
}

class State2 { 
    static handleA (): StateTypes { 
        // Do Something
        return State1; 
    }
    static handleB (): StateTypes {
        // Do Something
        return State2;
    }
}


let currentState: StateTypes = State1;

for (/* some Condition*/){
    if(/* some Condition*/)
        currentState = currentState.handleA();
    else
        currentState = currentState.handleB();
}

It works perfectly fine, however Typescript complains that it cannot find the static method handlaA() in class State1.

TS2339: Property 'handleA' does not exist on type 'StateTypes'.   Property 'handleA' does not exist on type 'State1'.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

type StateTypes = State1 | State2 means instance of State1 or State2. What you want is: type StateTypes = typeof State1 | typeof State2. This refers to constructors instead of instances

about 4 years ago · Juan Pablo Isaza Report

0

It seems that return State1 does not return what you expect it to. You can test that out in a simpler example:

class State2 { 
    static handleB (): State1 {
        return State1
    }
}

class State1 { 
    static test (): void {
        console.log("testing")
    }
}

Here we hope to get a reference to State1

let currentState = State2.handleB()
currentState.test()

But the error is the same: Property 'test' does not exist on type 'State1'.

You might solve it by making the state an instance. Then you can get a reference to a different state. You can overwrite it with an instance of the new state.

type currentState = State1 | State2

class State2 { 
    getNewState (): State1 {
        return new State1()
    }
    testMessage (): void {
        console.log("state two")
    }
}

class State1 { 
    getNewState (): State2 {
        return new State2()
    }
    testMessage (): void {
        console.log("state one")
    }
}


let currentState = new State2()

// ask for the new state 
currentState = currentState.getNewState()
currentState.testMessage()
about 4 years ago · Juan Pablo Isaza Report
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!