I've been working on creating a function that validates whether a key in an object is a non-empty string.
export const validateRequiredString = <T>(
obj: T,
key: keyof T & (string | number)
): void => {
if (typeof obj[key] !== "string") {
throw new Error(`${key} is not a string`);
}
if (obj[key] === "") {
throw new Error(`${key} is empty`);
}
};
However, I'm encountering an issue and can't figure out why it's not allowing me to make this work:
file.ts:8:7 - error TS2367: This condition will always return 'false' since the types 'T[keyof T & (string | number)]' and 'string' have no overlap