-
-
Notifications
You must be signed in to change notification settings - Fork 934
Closed
Labels
Description
PHPStan currently does not emit error when you pass non-never value to a parameter that requires never.
/** @param never $value */
function assertNever(mixed $value): never
{
throw new LogicException();
}
assertNever(123); // this should return error because 123 is not subtype of neverUse Case: exhaustive checks
This is commonly used for implementation of typesafe exhaustive checks in TypeScript. It is especially useful when dealing with enums, sealed classes or union types.
function testMissingCheck(int|string $value): void
{
if (is_string($value)) {
echo 'STRING';
} else {
assertNever($value); // this should return error, because int is not a subtype of never
}
}- PHPStan: https://phpstan.org/r/e5eaf74b-68ee-4ba2-be91-3f00bc2d6284
- TypeScript: Playground Link