Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
does not exist on type 'string'.ts(2339)

Im new to typescript, here i have been adding ts to my react project, i'm getting this kind of error: Property 'identifier' does not exist on type 'string'.ts(2339), the code works as it is but when added ts it gives that error, i can solve it like this: currentBroker: "" as any, but then thats not the right way. any help/suggestion ?

my code:

const initialSelectionState = {
  currentBroker: "",
};

export function selectionReducer(
  state = initialSelectionState,
  action: selectionReducerAction
) {
  switch (action.type) {
 case ActionType.CHANGE_SITE:
  return {
    ...state,
    currentSite: action.payload,
  };
case ActionType.CHANGE_CAMERA:
  return {
    ...state,
    currentCamera: action.payload,
  };
case ActionType.CHANGE_ANALYSER:
  return {
    ...state,
    currentAnalyser: action.payload,
  };
case ActionType.CHANGE_PLATFORM:
  return {
    ...state,
    currentPlatform: action.payload,
  };
case ActionType.CHANGE_BROKER:
  return {
    ...state,
    currentBroker: action.payload,
  };
case ActionType.CLEAR_ANALYSER_DATA:
  return {
    ...state,
    currentAnalyser: "",
  };
case ActionType.CLEAR_CAMERA_DATA:
  return {
    ...state,
    currentCamera: "",
  };
case ActionType.CLEAR_PLATFORM_DATA:
  return {
    ...state,
    currentPlatform: "",
  };
case ActionType.CLEAR_BROKER_DATA:
  return {
    ...state,
    currentBroker: "",
  };
    case ActionType.UPDATE_BROKER:
      const ucurrentBroker =
        state.currentBroker.identifier == action.payload.identifier
          ? action.payload
          : state.currentBroker;
      return { ...state, currentBroker: ucurrentBroker };
      }
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

currentBroker is of type string, it's not an object, and typescript tells you that you cannot access properties because it doesn't have them.

If you are using typescript I suggest you to declare interface of your state first:

interface IState {
   currentBroker: string;
}

const initialSelectionState: IState  = {
  currentBroker: "",
};

and of you need your broker to have an identifier property you can modify the state interface as you need:

interface IState {
   currentBroker: {
      identifier: string;
   }
}

const initialSelectionState: IState  = {
  currentBroker: {
    identifier: ""  
  };
};

With this you should not have the error again (if I understood correctly)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You initialise the "currentBroker" as a string.

const initialSelectionState = {
  currentBroker: "",
};

Then later attempt to access the currentBroker as if it's an object with a property identifier

state.currentBroker.identifier == action.payload.identifier

Casting to any will allow the code to run, and most likely just return undefined when trying to get that property.

If all you are trying to do is update a string in a state object, surely no logic is required?

const initialSelectionState = {
  currentBroker: "",
};

export function selectionReducer(
  state = initialSelectionState,
  action: selectionReducerAction
) {
  switch (action.type) {
    case ActionType.UPDATE_BROKER:
      return { ...state, currentBroker: action.payload};
  }
}

Since you mentioned the structure of the payload is as follows;

{
  identifier: 'b9b6381e-2d42-4953-b747-4b25fd382a04', 
  name: 'Broker 244', 
  host: 'localhost', 
  port: 1783, 
  site_id: '0-0-0-0'
}

Then your currentBroker should in some way match the structure of this. Alternatively, if there is 'no current broker initially', you should make it undefined. In either case, it definitely shouldn't be an empty string i.e. "" since that is a totally different data type.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda