-
Notifications
You must be signed in to change notification settings - Fork 650
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
False-positive TypeDoesNotContainType
while using conditional type for return
#8286
Comments
I found these snippets: https://psalm.dev/r/3a92e4a91f<?php
enum Enum {
case First;
case Second;
case Third;
}
class FirstId {}
class SecondId {}
class Test {
/**
* @return (
* $enum is Enum::First
* ? \Generator<FirstId>
* : (
* $enum is Enum::Second
* ? \Generator<SecondId>
* : never
* )
* )
**/
public static function method(Enum $enum): \Generator
{
match ($enum) {
Enum::First => yield new FirstId(),
Enum::Second => yield new SecondId(),
default => throw new \LogicException(),
};
}
}
/** @psalm-trace $firstIdGenerator **/
$firstIdGenerator = Test::method(Enum::First); // expected type is "Generator<mixed, FirstId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $secondIdGenerator **/
$secondIdGenerator = Test::method(Enum::Second); // expected type is "Generator<mixed, SecondId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $never **/
$never = Test::method(Enum::Third); // expected type is "never" got "Generator<mixed, FirstId, mixed, mixed>"
https://psalm.dev/r/cd28e96568<?php
enum Enum: int {
case First = 1;
case Second = 2;
case Third = 3;
}
class FirstId {}
class SecondId {}
class Test {
/**
* @param Enum $enum
*
* @return (
* $enum is Enum::First
* ? \Generator<FirstId>
* : (
* $enum is Enum::Second
* ? \Generator<SecondId>
* : never
* )
* )
**/
public static function method(Enum $enum): \Generator
{
match ($enum) {
Enum::First => yield new FirstId(),
Enum::Second => yield new SecondId(),
default => throw new \LogicException(),
};
}
}
/** @psalm-trace $firstIdGenerator **/
$firstIdGenerator = Test::method(Enum::First); // expected type is "Generator<mixed, FirstId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $secondIdGenerator **/
$secondIdGenerator = Test::method(Enum::Second); // expected type is "Generator<mixed, SecondId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $never **/
$never = Test::method(Enum::Third); // expected type is "never" got "Generator<mixed, FirstId, mixed, mixed>"
https://psalm.dev/r/4c57a77aa8<?php
enum Enum: string {
case First = 'first';
case Second = 'second';
case Third = 'third';
}
class FirstId {}
class SecondId {}
class Test {
/**
* @param Enum $enum
*
* @return (
* $enum is Enum::First
* ? \Generator<FirstId>
* : (
* $enum is Enum::Second
* ? \Generator<SecondId>
* : never
* )
* )
**/
public static function method(Enum $enum): \Generator
{
match ($enum) {
Enum::First => yield new FirstId(),
Enum::Second => yield new SecondId(),
default => throw new \LogicException(),
};
}
}
/** @psalm-trace $firstIdGenerator **/
$firstIdGenerator = Test::method(Enum::First); // expected type is "Generator<mixed, FirstId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $secondIdGenerator **/
$secondIdGenerator = Test::method(Enum::Second); // expected type is "Generator<mixed, SecondId, mixed, mixed>" got "Generator<mixed, FirstId, mixed, mixed>"
/** @psalm-trace $never **/
$never = Test::method(Enum::Third); // expected type is "never" got "Generator<mixed, FirstId, mixed, mixed>"
|
This is partially fixed by #9286. All traces produce expected results, but TypeDoesNotContainType is still there. |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
While trying to use a conditional type for a method return value based on the cases of an UnitEnum/BackendEnum, a
TypeDoesNotContainType
issue arises indicating that the value of parameter with the enum type could never be one of the enum cases.The issue exists for me while using
UnitEnum
,BackedEnum<int>
, andBackendEnum<string>
.Psalm Snippets
UnitEnum: https://psalm.dev/r/3a92e4a91f
BackedEnum (int): https://psalm.dev/r/cd28e96568
BackedEnum (string): https://psalm.dev/r/4c57a77aa8
PHPStan Snipppets
UnitEnum: https://phpstan.org/r/667b3feb-6a7e-4444-8666-64b37759c0d4
BackedEnum (int): https://phpstan.org/r/01028541-f166-4c1c-9ad1-f6c937fccb7f
BackedEnum (string): https://phpstan.org/r/04041665-afd1-4ebe-9b9b-5ed4725895ff
The text was updated successfully, but these errors were encountered: