mago
mago copied to clipboard
Negation type
🚀 Describe the Feature
It can be useful sometimes to indicate that an argument or return value should not be of a certain type (another way of narrowing the accepted input type or declared return type)
✅ Use Case / Example
<?php declare(strict_types=1);
/**
* @template I of scalar
* @template C of I&(literal-string|literal-int|false|true|null)
* @param I $input
* @param C&literal $canary
* @return ($input is $canary ? null : I&!C)
*/
function canaryValueToNull(mixed $input, mixed $canary): mixed
{
return $input === $canary ? null : $input;
}
function g(int|false $input): ?int
{
// should be OK
return canaryValueToNull($input, false);
}
/** @return non-empty-string */
function h(string $input): ?string
{
// should be OK
return canaryValueToNull($input, '');
}
function i(int|bool $input, float $f): ?int
{
// should not be OK (return value is int|true|null)
return canaryValueToNull($input, false);
}
function j(int|false $input): int
{
// should not be OK (return value is int|null)
return canaryValueToNull($input, false);
}
💡 Proposed Solution
No response
📌 Priority
Nice to have
📝 Additional Context
No response