Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Created July 25, 2024 10:20
Show Gist options
  • Save tjunghans/1b8b01cb0df293da0e09678423adc30c to your computer and use it in GitHub Desktop.
Save tjunghans/1b8b01cb0df293da0e09678423adc30c to your computer and use it in GitHub Desktop.
Typescript function to check if a given value is a valid port number
const isPortnumber = (value: number): boolean => {
// https://en.wikipedia.org/wiki/Registered_port
return value >= 0 && value <= 65535;
};
const isValidPortnumber = (value: unknown): value is number => {
if (typeof value === "number") {
return isPortnumber(value);
}
if (typeof value === "string") {
return isPortnumber(Number(value));
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment