Let's consider a scenario in which there is a JavaScript function as follows:
//index.js
function foo(obj) {
obj['bar'] = 'biz';
}
module.exports.foo = foo;
The challenge here is to create a TypeScript definition for this particular function.
An attempt has been made using the is
operator:
//index.d.ts
export function foo<T>(obj: T): T is T & { bar: string }
However, it seems that the type system is unable to recognize bar
as a valid key within whatever object is passed to foo
.
How can this issue be resolved?