0

Here is the JS I have trying to convert to TS:

/**
 * @param {string} value The value to be formatted into a phone number
 * @returns {string}
 */
export const formatPhoneString = (value) => {
const areaCode = value.substr(0, 3);
const prefix = value.substr(3, 3);
const suffix = value.substr(6, 4);
const output = `(${areaCode}) ${prefix}-${suffix}`;

return output;
};

/**
* Event handler for a phone number input to update the phone number's formatting
* @param {object} event js event object
*/
export const formatPhoneNumber = (event) => {
const targetElement = event.target;

// Get the value and remove all of the non-digit characters
const value = targetElement.value.replace(/\D/g, '');

if (value.length === 10) {
    const output = formatPhoneString(value);

    // Update the value of the input
    targetElement.value = output;
}`enter code here`
};

When I try to pull this into new ts file getting type error for "event" What is the syntax for the event object in TS?

MRF88
  • 3
  • 1
  • What type did you set? There is an Event type in TS. (Don't know the full type of the top of my head, but shouldn't be hard to find) – cloned Oct 20 '22 at 20:34

1 Answers1

0

You should set it to type Event, KeyboardEvent, MouseEvent, etc

export const formatPhoneNumber = (event: Event) => {

}
Archigan
  • 62
  • 11
  • https://stackoverflow.com/questions/42081549/typescript-react-event-types – Archigan Oct 20 '22 at 20:35
  • Thanks That helped with my first issue. Second question is how to replace "targetElement.value". Getting error Value not found on Event – MRF88 Oct 24 '22 at 17:04