-
Notifications
You must be signed in to change notification settings - Fork 653
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
A way to use signature of another method as a type #8716
Labels
Comments
I found these snippets: https://psalm.dev/r/bd50e7e47e<?php
trait Creatable
{
/** @param method-args<static::__construct> $args */
public static function create(...$args): static
{
return new static(...$args);
}
/** @param method-args<static::__construct> $args */
public static function createIf(bool $condition, ...$args): static|null
{
return $condition ? new static(...$args) : null;
}
}
final class A
{
use Creatable;
public function __construct(private int $param) {}
}
final class B
{
use Creatable;
public function __construct(private string $param) {}
}
$a = A::create(2); // valid
$b = B::createIf(true === true, "42"); // valid
$aa = A::createIf(true, "42"); // invalid
$bb = B::create(2); // invalid
|
Could be extended to functions and callable, and include return types: /**
* @param function-args<$func> $args
* @return function-return<$func>
*/
function call_user_func(callable $func, ...$args) { ... } |
weirdan
changed the title
A way to copy signature of another method as a type
A way to use signature of another method as a type
Nov 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rationale
Some methods may have signatures based on other methods. One particular example is Laravel's
Dispatchable
trait, where methods have signatures based on constructor signature.It would be great if we had a way to specify something like this:
https://psalm.dev/r/bd50e7e47e
Prior art
Parameters<T>
,ConstructorParameters<T>
andReturnType<T>
in Typescript.Typescript, however, builds those types on function signature types + generics +
infer
: https://github.com/microsoft/TypeScript/blob/a3092c798ad9f165b0f7cba964a2c7b976cd30d0/lib/lib.es5.d.ts#L1601The text was updated successfully, but these errors were encountered: