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

[New Feature] Extend ArrayShape with ArrayShape #11061

Open
marartner opened this issue Jul 30, 2024 · 1 comment
Open

[New Feature] Extend ArrayShape with ArrayShape #11061

marartner opened this issue Jul 30, 2024 · 1 comment

Comments

@marartner
Copy link

There currently does not seem to be a working way to extend a known array shape with additional keys, other than redefining it entirely. "..." can be used to signal that there may be additional keys, but not which:

I would propose this solution:

https://psalm.dev/r/3afa0bf3db

Copy link

I found these snippets:

https://psalm.dev/r/3afa0bf3db
<?php


/**
 *  @psalm-type FooAsArray = array{foo:int, bar:string}
 *  @psalm-type BarAsArray = array{foobar: string, ...FooAsArray}
 */


/** @psalm-return FooAsArray */
function foo(): array {
    return ['foo'=> 1, 'bar' => 'baz'];
}

/** @psalm-return BarAsArray */
function foobar(): array {
    $arr = foo();
    $arr['foobar'] = 'bazbar';
    return $arr;
}

function test(): array {
    $val = foobar();
    /** @psalm-trace $val */
    return $val;
}

// I currently get array{foobar: string, ...<array-key, mixed>}
// I would have expected array{foobar: string, foo:int, bar:string}
Psalm output (using commit 16b24bd):

INFO: Trace - 25:5 - $val: array{foobar: string, ...<array-key, mixed>}

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

1 participant