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

Intersection types don't allow arrays #7600

Open
danog opened this issue Feb 7, 2022 · 5 comments
Open

Intersection types don't allow arrays #7600

danog opened this issue Feb 7, 2022 · 5 comments

Comments

@danog
Copy link
Collaborator

danog commented Feb 7, 2022

I find it often useful to use intersection types to enhance the keys of an array, but psalm doesn't like it: https://psalm.dev/r/125b383b8b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/125b383b8b
<?php

/**
 * @template T as array
 * @param T $i
 * @return T&array{test1: "test"}
 */
function enhance(array $i): array {
    $i['i'] = 'test';
    return $i;
}
Psalm output (using commit faad966):

ERROR: InvalidDocblock - 8:1 - Intersection types must be all objects, Psalm\Type\Atomic\TKeyedArray provided in docblock for enhance

@orklah
Copy link
Collaborator

orklah commented Feb 7, 2022

This is just missing support for intersections between templates and keyed array. The intersection in itself is ok: https://psalm.dev/r/9384c60365

This should probably not be hard to fix and we have one or two similar issues (intersection with aliased types)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9384c60365
<?php

/**
 * @template T as array
 * @param T $i
 * @return array&array{test1: "test"}
 */
function enhance(array $i): array {
    $i['i'] = 'test';
    return $i;
}
Psalm output (using commit faad966):

ERROR: InvalidReturnStatement - 10:12 - The inferred type 'array{i: 'test'}' does not match the declared return type 'array{test1: 'test'}' for enhance

ERROR: InvalidReturnType - 6:12 - The declared return type 'array{test1: 'test'}' for enhance is incorrect, got 'array{i: 'test'}'

@kkmuffme
Copy link
Contributor

Especially with v5 unsealed arrays and restrictReturnTypes this is a more common issue now

@kkmuffme
Copy link
Contributor

kkmuffme commented Dec 1, 2023

It seems this worked previously? #3064

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

3 participants