#What should I be using intense type checking for?

6 messages · Page 1 of 1 (latest)

latent mica
#

Sure I could implement it for everything, but if I am fetching data from an internal/on-prem server and I know the data coming back, should I be type checking the data that is fetched? Are there general rules for making types and when to use types/nested types?

Lets say I have a UI that searches for info based on a phone number or mac address, I would type check the UI input as sort of a sanitization process right? But the data that input fetches does not need to be type checked?

I guess I'm wondering when I should be trying to make intense type checking like so:

type TPhoneNumber = validatePhoneNumber(); // returns string that fits regex for a phone number or something...
type TMacAddress = validateMacAddress(); // similar to above but with regex for mac address...

type TKeyInfo = {
  phoneNumber?: TPhoneNumber;
  macAddress?: TMacAddress;
}

type TKey = 
  | (TKeyInfo & { phoneNumber: TPhoneNumber})
  | (TKeyInfo & { macAddress: TMacAddress});

type TFormData = {
  searchKey: TKey;
  ...OtherFormInputs...
}

Does this seem like good practice, or is it too verbose and pointless?

I want to have a form input that takes either a phone number string or a mac address string, has at least one but can have multiple (running sql searches based on these 'keys').

tribal tangle
#

@latent mica Mostly, I'd suggest keeping the types simple until there's a specific reason to make them more complicated.

#

If you're finding it hard to keep track of what part of the code is responsible for validating the phone number of the mac address; it can make sense to try to express that validation as part of the type-system (though it's a bit tricky) - but if the validation is just done once when it comes from the API, expressing that nuance in the type system may be unnecessary.

latent mica
#

The backend does not validate the input, and I don't think it will be changed to do so.

I should clarify that the data fetching should probably be a whole different question. I am just wondering about how I should type things for a react form that searches via different types of keys (phone number or Mac address)

tribal tangle
#

I'd probably keep it simple:

type SearchData = {
   kind: "phoneNumber" | "macAddress";
   value: string
}
latent mica
#

I was hoping to find a different way than just having an input field for each and only use one input field