-
Notifications
You must be signed in to change notification settings - Fork 663
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
Intersection with an imported type #5148
Comments
I found these snippets: https://psalm.dev/r/ec2d56f41d<?php
/** @psalm-type X = array{x: int} */
class A {}
/** @psalm-import-type X from A
@psalm-type Y = array{y: int}
@psalm-type Q = X&Y */
class B {}
|
A possible workaround would be to introduce a dummy helper class with the second type definition and to import both types and only then perform the intersection on them. |
I found these snippets: https://psalm.dev/r/8ea579671b<?php
/** @psalm-type X = array{x: int} */
class A {}
/** @psalm-type Y = array{y: int} */
class _ {}
/** @psalm-import-type X from A
@psalm-import-type Y from _
@psalm-type Q = X&Y */
class B {}
|
yet, it doesn't work if you try to use |
I found these snippets: https://psalm.dev/r/8ea579671b<?php
/** @psalm-type X = array{x: int} */
class A {}
/** @psalm-type Y = array{y: int} */
class _ {}
/** @psalm-import-type X from A
@psalm-import-type Y from _
@psalm-type Q = X&Y */
class B {}
https://psalm.dev/r/1eaa0c447f<?php declare(strict_types=1);
/** @psalm-type X = array{x: int} */
class A {
/** @var X */
protected $foo = ['x' => 10];
}
/** @psalm-type Y = array{y: int} */
class _ {
/** @var Y */
protected $foo = ['y' => 10];
}
/**
* @psalm-import-type X from A
* @psalm-import-type Y from _
* @psalm-type Q = X&Y
*/
class B {
/** @var X */
protected $x = ['x' => 10];
/** @var Y */
protected $y = ['y' => 10];
/** @var Q */
protected $q = ['x' => 1, 'y' => 2];
/** @psalm-return Q */
function getQ(): array
{
return $this->q;
}
/** @psalm-param Q $input */
function setQ(array $input): void
{
$this->q = $input;
}
}
|
Error was clarified, intersections with array shapes are not supported |
@orklah, but intersections with array shapes are supported, aren't they? It's just the intersections with imported array shape types that cause problems. And why would you actually not support intersections with imported types? It just feels like a natural thing. It's like if you could include a file in PHP bud you couldn't call included functions inside of other functions and the PHP developer's solution would be to just clarify the error that it is not supported. |
You're right, I was confused, the basic version works, but here, the second example doesn't either: https://psalm.dev/r/9ddbcacb2f The error is still kinda misleading. I'll flag this as enhancement |
I found these snippets: https://psalm.dev/r/9ddbcacb2f<?php
/**
* @psalm-param array{a: string}&array{b: int} $_data
*/
function intersect(array $_data) :void {
}
/**
* @psalm-param T as array{b: string}
* @psalm-param array{a: string}&T $_data
*/
function intersect2(array $_data) :void {
}
|
I just encountered this bug in a situation like https://psalm.dev/r/324fd96838 This shouldn't be too hard to fix, since it is working with the typeAlias last https://psalm.dev/r/89e0dffd1b |
I found these snippets: https://psalm.dev/r/324fd96838<?php
namespace SonataFoo;
/**
* @phpstan-type SuperFoo = array{foo: int}
*/
class Foo {}
namespace SonataBar;
/**
* @phpstan-import-type SuperFoo from \SonataFoo\Foo
* @phpstan-type SuperBar = SuperFoo&array{bar: int}
*/
class Bar {}
https://psalm.dev/r/89e0dffd1b<?php
namespace SonataFoo;
/**
* @phpstan-type SuperFoo = array{foo: int}
*/
class Foo {}
namespace SonataBar;
/**
* @phpstan-import-type SuperFoo from \SonataFoo\Foo
* @phpstan-type SuperBar = array{bar: int}&SuperFoo
*/
class Bar {}
|
https://psalm.dev/r/ec2d56f41d
When I try to make an intersection type with an imported type, I get the following error:
Isn't
TKeyedArray
an object-like array? This seems like a bug to me, but maybe I'm not getting something right.The text was updated successfully, but these errors were encountered: