I have a function that returns whether an object property is undefined
. I need this function instead of just doing obj[key] === undefined
because otherwise I’d sometimes get Property 'foo' does not exist on type 'Bar'.
. It’s straightforward to write the type when the property key is a literal. I.e.:
function hasDefinedProp<
Obj extends Partial<Record<string, any>>,
Prop extends string,
>(
obj: Obj,
prop: Prop,
): obj is Obj & Record<Prop, Prop extends keyof Obj ? Exclude<Obj[Prop], undefined> : unknown> {
return obj[prop] !== undefined;
}
const obj: Partial<Record<string, number>> = {};
if (hasDefinedProp(obj, 'foo')) {
obj.foo + 1; // No error.
obj.bar + 1; // "Object is possibly 'undefined'."
}
However, this doesn’t work when the key’s type is a wide type, i.e.:
const obj: Partial<Record<string, number>> = {};
const key: string = '';
if (hasDefinedProp(obj, key)) {
obj[key] + 1; // No error.
obj.bar + 1; // No error. Should be "Object is possibly 'undefined'."
}
Is it possible to make the type guard work for wide types?