Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda