As I work with a code generator that produces strings, I'm encountering this issue:
const pageUrl = generator.next().value;
const result = catalogPageParser(pageUrl);
An error is being thrown:
Argument of type 'string | void' is not assignable to parameter of type 'string'.
Type 'void' is not assignable to type 'string'.ts(2345)
To address this problem, one option is to adjust the variable like this:
const pageUrl = generator.next().value ?? "some valid url";
Alternatively, the signature of the catalogPageParser
function could be altered. However, neither of these solutions seem ideal to me. Are there any other suggestions for correctly typing this?
I am hesitant to use a default value as there is no suitable default in my scenario. Furthermore, allowing void as a valid argument for the function doesn't seem appropriate either.
Example
function* someGenerator() {
yield "url1";
yield "url2";
}
function acceptsStringOnly(argument: string) {
console.log(argument);
}
const value = someGenerator().next().value;
acceptsStringOnly(value);