For my unit tests I create a fake window.location
object. Here’s the slightly simplified code:
function getFakeLocation(getCurrentUrl: () => URL): Location {
return {
get ancestorOrigins(): DOMStringList {
return {
length: 1,
contains: (origin: string) => origin === getCurrentUrl().origin,
item(index: number) {
if (index === 0) {
return getCurrentUrl().origin;
} else {
return null;
}
},
*[Symbol.iterator]() {
yield getCurrentUrl().origin;
},
};
},
assign: vi.fn((url: string) => {
pushState({}, new URL(url, getCurrentUrl()));
}),
get href() {
return getCurrentUrl().href;
},
set href(url: string) {
pushState({}, new URL(url, getCurrentUrl()));
},
// …
};
}
This worked fine until I upgraded from TypeScript 5.4.2 to 5.6.0-beta. Now, tsc
complains about the ancestorOrigins
definition above:
Type '() => Generator<string, void, any>' is not assignable to type '() => BuiltinIterator<string, undefined, any>'.
Call signature return types 'Generator<string, void, any>' and 'BuiltinIterator<string, undefined, any>' are incompatible.
The types returned by 'next(...)' are incompatible between these types.
Type 'IteratorResult<string, void>' is not assignable to type 'IteratorResult<string, undefined>'.
Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorResult<string, undefined>'.
Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorReturnResult<undefined>'.
Type 'void' is not assignable to type 'undefined'.
*[Symbol.iterator]() {
~~~~~~~~~~~~~~~~~
I already updated @types/node
to version 20.16.1 as per this question which solved another type error but the above error still remains. However, to me the latter sounds very similar to what Daniel Rosenwasser wrote in his answer in that thread.