Discriminated union with an optional discriminator is not being resolved as expected #58508
Labels
Experimentation Needed
Someone needs to try this out to see what happens
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
🔎 Search Terms
discriminated union, discriminated union with an optional discriminator, type inference in objects
🕗 Version & Regression Information
I have a simple piece of code (see the playground/below)
The runtime flow is very simple: whatever gets returned by the loader func gets passed as
data
to thehandle
'scrumbBuilder
method. If the loader is absent thendata
will also be absentBut when it comes to typing those behaviors then 2 issues arise:
any
data
type, however from theloader
definition it is possible to infer its return type. Is there a syntax way to say 'the type of data is the whatever awaited type the loader function returns?. I have tried to achieve that behaviour using generics and the
satisifies` keyword but failed to⏯ Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBACghsAFgZQgcwLYQHbCgXigHsAjAKwgGNgBtAXQG4BYAKFdEigAk5sATADYQAPABUAIgjgA+AlADerKMqiUATgFcMJAEIaAlgL4Q1ALigAKPlPMSpASgKz4SVJhy1GrAL6t24aAAZIjhjNTFJYBk5Czg1NABnc3koMDi4DCSoACUqIjU+YQTgNX1sNAAaKGLS8tlvR3xnNSIMfQSROyjpZjYWDmhsog1gCB5+IXCuuDleEFl8JRUAHwUllRVEXkEIc3Gd4WwIADcTHvWNqAEQsIB+cyPTtV6N3xYN1cV3y+Utid3uNshBEpOdvj9rqETOZglCppEZBdvL1WJQiNhilAWiMxkCTAB1fRIYbAWFhORfDZ-HbJC4bdRaXQGIzQyzWKKNWT0KoAeh57Jm7VU+TUVGAAhAUDKADMTKK+FA4AkoI8TEifNUEO1pfoIMqhjj9pM-Cw0Ri8NjRkaTBSLpCwuYlSBsJRLHFEpy1uDLqLgBo1NhFfEEgA6NJqDKhjQdNQAST4LxU3gqF2pQlp3vpmm0ekMDrZDicUHoUD5AqlyuwRDwMrlEAVSuqJTKaHVLG8muA2t1+pJuP+4RqLekQA
💻 Code
🙁 Actual behavior
data
is not inferred as string🙂 Expected behavior
data should be inferred as a string
there is no need to explicitly annotate or assert the
data
type , and it must be inferred from the loader return typeAdditional information about the issue
No response
The text was updated successfully, but these errors were encountered: