generator next type should be inferred as union (instead of intersection) of yields' types OR just unknown #58343
Labels
Needs Investigation
This issue needs a team member to investigate its status.
π Search Terms
generator
yield
next
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.0-beta&filetype=ts#code/GYVwdgxgLglg9mAVAAgOYFMzoE4EMpzYAUAlMgN4BQyyECAzlMgJ4zoA2AJgIwBcyjbDDCpkAXhZsu1Wgzjt0AOnZxURcpI49kAXxIy6YRpq4AmfmBABbAEY5xJzgbkLlq9Y9O79NQ8dZaAMz8NnDy6LhgDgHSvi5KKmoaMZyB3pQ6lJR+TBhREnk4+ISklHmKWAAeUKVlmBXo1UQA5ILCqM1kAPRdyAAqzAAO6MitUEIizcgw9MhgcEy49PQwqGC4NgrIBNtDI81YAG44zZQ9FI58o22TunVgDU2m3b0Dw6OWtifTs-OLy6t1psRjsoHsPuhjthTudklJOOZkF5MuUqjVxiB0C9+uDmqFwpEpjM5gtkEsVmsNltQbijiczr04UF+BiRpkgA
π» Code
the following is perfectly valid javascript
but becomes a type error when converted to the equivalent typescript
π Actual behavior
generator
's inferred type signature isbecause the
Generator
'sTNext
gets inferred as the intersection of theyield
s' types:string & number & boolean
->never
π Expected behavior
generator
's inferred type signature should be the union of theyield
s' typesOR
just
unknown
causing
(yield) as string
(along with the usage of any type to whichunknown
is not assignable) to be a compilation errorAdditional information about the issue
while inferring the union still isn't perfectly type-safe (e.g. the inferred union cannot statically prevent a
const yield1: string = yield
from being incorrectly called withnext(2)
), it would go beyond bringing the inferred version up to parity with the explicit version because the explicit version would not be able to type eachyield
independently:forcing each
yield
to be typed as the intersection of allyield
s' respective (desired) type may in fact be desirable becausebut if this is the case, perhaps generators should never have their types inferred and instead default to all
TNext
s/yield
s being typed asunknown
, thereby prompting the developer to provide their explicit types when more precision is needed. this would also promote saferyield
consumption, e.g.prompts the developer to rewrite it as
and eventually
The text was updated successfully, but these errors were encountered: