Does PHPStan has an equivalent to TypeScript's unknown? #7194
BastienClement
started this conversation in
General
Replies: 2 comments 5 replies
-
Just a answer on this particular thing:
You might use https://github.com/staabm/phpstan-dba in case you are using mysqli or pdo_mysql as a db connector (pdo_pgsql will be supported soon) to make phpstan aware of the database types |
Beta Was this translation helpful? Give feedback.
1 reply
-
You might have some luck by returning StrictMixedType from dynamic return type extensions. This is how level 9 works - it transforms explicit MixedType into StrictMixedType. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there,
As I currently understand it, PHPStan allows us to select one of two behavior for
mixed
is_bool
)If I'm not mistaken, the first case matches the
any
behavior from TypeScript, while the second matchesunknown
.In our specific use case, we are not able to enable level 9 globally on our project since a lot of legacy code is written around
mixed
and fully typing it is an unachievable goal. However in some places, we actually do have an unknown value coming from some database or external service and would like to ensure that every usage of it is correctly guarded by type-checking code at runtime.In this case, TypeScript let developers select the top-type behavior in a case by case basis, while it seems that PHPStan forces a single behavior tied to a global config toggle.
Is there a way to achieve a similar strict-top-type in PHPStan, or would something similar be achievable with a reasonable effort by somehow enabling the strict mixed behavior on a case by case basis ?
Beta Was this translation helpful? Give feedback.
All reactions