Skip to content
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

Does totallyTyped need to be SO strict? #2024

Closed
zerkms opened this issue Aug 16, 2019 · 3 comments
Closed

Does totallyTyped need to be SO strict? #2024

zerkms opened this issue Aug 16, 2019 · 3 comments

Comments

@zerkms
Copy link
Contributor

zerkms commented Aug 16, 2019

It looks like totallyTyped is being paranoid without a good reason.

Let's take this code for example (https://psalm.dev/r/715ca196f9):

<?php

function keys(): array
{
  return ['foo', 'bar'];
}

foreach (keys() as $k) {
  echo gettype($k);
}

Nothing there requires the $k to be of a known type, the gettype accepts a mixed argument.

Yet, there is no (at least I could not find) way to satisfy the linter and keep the type of the array values unknown (or rather - irrelevant).

I have found something relevant #652 but I still struggle to see how it would be possible to type this example.

@muglug
Copy link
Collaborator

muglug commented Aug 16, 2019

Short answer: yes, it does, because Psalm isn't that smart - it doesn't know that you're not going to do this in the loop:

function keys(): array {
  return ['foo', 'bar'];
}

foreach (keys() as $k) {
  echo $k * 4;
}

@muglug muglug closed this as completed Aug 16, 2019
@zerkms
Copy link
Contributor Author

zerkms commented Aug 16, 2019

@muglug

how would one type it though?

For the very example I provided (where I want to echo the type of a variable and the return type of keys is array<array-key, mixed>).

Is it even possible to type it?

@zerkms
Copy link
Contributor Author

zerkms commented Aug 16, 2019

Btw,

that you're not going to do this in the loop:

Then it would emit another warning for the $k * 4 statement explicitly.

To me mixed should behave like unknown in TS, hence an initialisation of a variable with a mixed value should end up in creating a variable of mixed type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants