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

Allow importing type aliases on methods/functions #4807

Open
weirdan opened this issue Dec 8, 2020 · 5 comments
Open

Allow importing type aliases on methods/functions #4807

weirdan opened this issue Dec 8, 2020 · 5 comments

Comments

@weirdan
Copy link
Collaborator

weirdan commented Dec 8, 2020

Currently Psalm processes @psalm-import-type in docblocks of classlikes only. It would be useful to be able to import types in function docblocks as well.

https://psalm.dev/r/138af4f19d
Expected: no issues

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/138af4f19d
<?php
namespace A {
    /** @psalm-type _AType=int|string */
    final class Types {}
}
namespace B {
    use A\Types;    
    /** 
     * @psalm-import-type _AType from Types as _Q
     * @param _Q $p
     */
    function f($p): void {}

    class C {
       /** 
        * @psalm-import-type _AType from Types as _Z
        * @param _Z $p
        */
        public function f($p): void {}
    }
}
Psalm output (using commit 91ee4ae):

ERROR: UndefinedDocblockClass - 10:15 - Docblock-defined class or interface B\_Q does not exist

ERROR: UndefinedDocblockClass - 17:18 - Docblock-defined class or interface B\_Z does not exist

@thomasvargiu
Copy link
Contributor

I think it could be related to #4290, it's not even possibile to use them with templates for classes.

@louisreingold
Copy link

Also related to #3511

@rzvc
Copy link

rzvc commented May 12, 2022

Is there any workaround for this?

@psalm-type seems to be a very powerful utility, but it suffers from a couple of crippling limitations. The one in this issue being one of them.

@jwdunne
Copy link

jwdunne commented Sep 8, 2022

Is there any workaround for this?

@psalm-type seems to be a very powerful utility, but it suffers from a couple of crippling limitations. The one in this issue being one of them.

For functions, there is a not-very-elegant work around. Given a file of functions, it's possible to declare an empty, throwaway class at the top where you can import types, which can then be used by any other functions in the file. Example:

/**
 * @psalm-import-type Type from Types
 */
class ImportedTypes
{
}

/**
 * @param Generator<int, Type> $xs
 * @return Generator<int, Type>
 */
function something(Generator $xs): Generator
{
    // ...
}

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

No branches or pull requests

5 participants