Closed
Description
TypeScript Version: 3.7.2, nightly
Search Terms:
Generator can't infer that string belongs to a union in return type
Code
type FooType = { a: 'foo' };
type G = Generator<any, FooType, any>;
function* broken(): G {
return {
a: 'foo'
/*
Type 'string' is not assignable to type '"foo"'.(2322)
input.ts(1, 18): The expected type comes from property 'a' which is declared here on type 'FooType'
*/
}
}
function* workaround1(): G {
const rval: FooType = { a: 'foo' };
return rval;
}
function* workaround2(): G {
return {
a: 'foo' as const
}
}
Expected behavior:
The return type shouldn't need these workarounds to know that 'foo' belongs to the union type.
Actual behavior:
Error described above. I included a couple workarounds that don't seem like they should be necessary, but are.
Related Issues: None as far as i could find