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

217
Views
El método estático mecanografiado no existe en el tipo someclass

Tengo un código como este -

 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(); }

Funciona perfectamente bien, sin embargo, Typescript se queja de que no puede encontrar el método estático handlaA() en la clase 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 significa instancia de State1 o State2 . Lo que quieres es: type StateTypes = typeof State1 | typeof State2 . Esto se refiere a constructores en lugar de instancias.

about 4 years ago · Juan Pablo Isaza Report

0

Parece que return State1 no devuelve lo que esperas. Puedes probar eso en un ejemplo más simple:

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

Aquí esperamos obtener una referencia a State1

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

Pero el error es el mismo: Property 'test' does not exist on type 'State1'.

Puede resolverlo haciendo del estado una instancia. Entonces puede obtener una referencia a un estado diferente. Puede sobrescribirlo con una instancia del nuevo estado.

 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!