更新时间:2021-12-12 00:33:18
This is an open issue in typescript (https://github.com/microsoft/TypeScript/issues/4742).自动类型保护和泛型并不总是一起工作.作为一种可能的解决方法,您可以手动定义此类型保护:
This is an open issue in typescript (https://github.com/microsoft/TypeScript/issues/4742). Automatic type guards and generics don't always work together. As a possible work around you could define this type guard manually:
type SomeType = {
foo: string;
} | undefined;
function isDefined<T> (v: T): v is Exclude<T, undefined> {
return v !== undefined
}
function someFn<TParams extends SomeType>(params1: SomeType, params2: TParams): void {
if (params1) {
Object.entries(params1);
}
if (isDefined(params2)) {
Object.entries(params2);
}
}