• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

239
Views
no existe en el tipo 'string'.ts(2339)

Soy nuevo en mecanografiar, aquí he estado agregando ts a mi proyecto de reacción, recibo este tipo de error: la propiedad 'identificador' no existe en el tipo 'cadena'.ts (2339), el código funciona como está pero cuando se agrega ts da ese error, puedo resolverlo así: currentBroker: "" as any, pero esa no es la forma correcta. alguna ayuda/sugerencia?

mi código:

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

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

currentBroker es de tipo cadena, no es un objeto, y mecanografiado le dice que no puede acceder a las propiedades porque no las tiene.

Si está utilizando mecanografiado, le sugiero que primero declare la interfaz de su estado:

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

y si necesita que su corredor tenga una propiedad de identifier , puede modificar la interfaz de estado según lo necesite:

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

Con esto no debería volver a tener el error (si entendí bien)

almost 3 years ago · Juan Pablo Isaza Report

0

Inicializas el "currentBroker" como una cadena.

 const initialSelectionState = { currentBroker: "", };

Luego, más tarde intente acceder a currentBroker como si fuera un objeto con un identifier de propiedad

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

La conversión a cualquiera permitirá que se ejecute el código, y lo más probable es que solo devuelva undefined al intentar obtener esa propiedad.

Si todo lo que está tratando de hacer es actualizar una cadena en un objeto de estado, seguramente no se requiere lógica.

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

Como mencionó, la estructura de la carga útil es la siguiente;

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

Entonces su currentBroker debería coincidir de alguna manera con la estructura de this. Alternativamente, si no hay 'ningún corredor actual inicialmente', debe hacerlo undefined . En cualquier caso, definitivamente no debería ser una cadena vacía, es decir, "" , ya que es un tipo de datos totalmente diferente.

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error