class Foo {
/** @type string */
a = '';
}
class Bar {
/** @type string */
a = '';
/** @type string */
b = '';
}
const fooBar = {
foo: new Foo(),
bar: new Bar(),
}
/**
* @param {'foo'|'bar'} fooOrBar
* @returns {Foo|Bar}
*/
const getFooBar = (fooOrBar) => fooBar[fooOrBar];
/**
* @returns {Foo}
*/
const getFoo = () => getFooBar('foo');
/**
* @returns {Bar}
*/
const getBar = () => getFooBar('bar');
getBar is complaining because Type 'Foo | Bar' is not assignable to type 'Bar'. Property 'b' is missing in type 'Foo' but required in type 'Bar'. . That error makes perfect sense, but how can I set up the jsdoc comments so that there are no complaints since this is technically fine?
(obviously this is a simpler example to showcase the problem, but I have more complex logic that is essentially the same that I'd like to be able to have with "no complaints" without instead having to duplicate basically all of getFooBar within each getFoo and getBar)