【发布时间】:2022-01-15 06:48:53
【问题描述】:
我正在尝试编写一个函数来验证对象的键是否为字符串且不为空。
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`);
}
};
但不知道为什么它不让我写这个
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
【问题讨论】:
标签: typescript typescript-generics