any lets anything through and can hide bugs. unknown is safer: you must check before you use it. Here you will accept an unknown value and return uppercase text only when it is a string.
Use typeof x === 'string' to narrow. Return a default when the value is not a string. Keep the function small and pure.
function toLowerIfString(x: unknown) {
return typeof x === "string" ? x.toLowerCase() : "";
}
Implement a function that uppercases a value only when it is a string.