Open thomasvargiu opened 4 years ago
I found these snippets:
Ok, this might be grounds for a slightly breaking change (that would bring Psalm more in line with PHPStan).
I like TypeScript's behaviour, where specifying:
function identity<T extends string|object|boolean|void|undefined|number|null>(t: T) : T {
return t;
}
let a = identity('a');
if (a === 'b') {} // a type error
vs
function identity<T /** implicitly extends any */>(t: T) : T {
return t;
}
let a = identity('a');
if (a === 'b') {} // allowed
So in Psalm
/**
* @template A as string
* @param A $a
* @return callable(A): A
*/
function foo($a): callable
{
return fn ($b) => $b;
}
foo('a')('b'); // forbidden
/**
* @template A
* @param A $a
* @return callable(A): A
*/
function foo($a): callable
{
return fn ($b) => $b;
}
foo('a')('b'); // allowed
cc @ondrejmirtes @TysonAndre
I'm trying to write stubs for a liibrary and I have some problems with literal strings. I just need to compare the type and not the value.
How can I do something like that?
This is a simple example where I would like that the callable should return the same type of the input param, but not the same. There is a way to do it? Maybe would be nice some magic type like
non-literal<A>
?https://psalm.dev/r/915c2c72eb