-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for generics in interface inference #270
Comments
Hi @arnaud-lb, thank you for reporting this issue; I did not have this usecase in mind when pushing 1b0ff39 indeed. From my POV, the following should make sense, although it is not supported by the library I certainly could make this work: /** @return class-string<Root<NodeA>|Root<NodeB>> */
function inferRootType(string $type): string {
return match ($type) {
'NodeA' => 'Root<NodeA>',
'NodeB' => 'Root<NodeB>',
default => throw new \Exception(),
};
} But static analysis tools wont be happy about that, see PHPStan example and Psalm example; this probably makes sense as Do you see any other way than using string values like BTW big fan of your work on PHPStan generics. 🙂 |
Thank you :D I wish you and this project a happy new year :)
Agreed. I think that the
Introducing a new Adding support for Using string values like |
Thank you for the awesome work on this library. Valinor is super useful and makes it much easier to deal with external data in a strongly typed project.
I am facing an issue since 0.11 added the following restriction:
Currently, this change makes interface inference incompatible with generic classes, as Valinor will not accept a return type like
class-string<Foo<A>|Foo<B>>
(rightfully, as this is not a valid type) or'Foo<A>'|'Foo<B>'
.To give some context, I'm trying to map a discriminated union, but the discriminator is on the parent node.
The source looks like this:
And the model looks like this:
Due to the position of the discriminator, I can not directly infer the type of the nested node: I must infer the type of the root node.
It seems that supporting a return type that consists of a union of strings in this case would fix the issue, as I could type the mapper like this:
Here is a full example of my use-case: https://gist.github.com/arnaud-lb/535d89874ed719265f66bc19a88b5fb0
The text was updated successfully, but these errors were encountered: