-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Is T[K] broken now? #9822
Labels
Comments
I found these snippets: https://psalm.dev/r/be7148dbb1<?php
/**
* @template T of array
* @template TKey of string
* @param T $arr
* @param TKey $k
* @return T[TKey]
*/
function a(array $arr, string $k): mixed {
assert(isset($arr[$k]));
return $arr[$k];
}
$a = a(['test' => 123], 'test');
/** @psalm-trace $a */; // 123
|
Well, it seems to work but Mixed* issues happens. We should try to get rid of those |
It doesn't work here too? https://psalm.dev/r/3d33f66c11 (like https://psalm.dev/r/e4eb7a3e05 doesn't work either but there seems to be a separate bug psalm/psalm.dev#98) |
I found these snippets: https://psalm.dev/r/3d33f66c11<?php
class QueryDB {}
class Compatibility {}
class DI {
const MAPPING = array(
'query' => QueryDB::class,
'comp' => Compatibility::class,
);
/**
* @template TMapping of DI::MAPPING
* @template TKey of key-of<DI::MAPPING>
*
* @param TKey $arg
* @return TMapping[TKey]
*/
public function get( $arg ) {
$v = self::MAPPING[ $arg ];
/** @psalm-trace $v */;
return $v;
}
}
$d = new DI();
$r = $d->get( 'comp' );
$c = new $r();
/** @psalm-trace $c */;
https://psalm.dev/r/e4eb7a3e05<?php
class QueryDB {}
class Compatibility {}
class DI {
const MAPPING = array(
'query' => QueryDB::class,
'comp' => Compatibility::class,
);
/**
* @template TMapping of DI::MAPPING
* @template TKey of key-of<TMapping>
*
* @param TKey $arg
* @return TMapping[TKey]
*/
public function get( $arg ) {
$v = self::MAPPING[ $arg ];
/** @psalm-trace $v */;
return $v;
}
}
$d = new DI();
$r = $d->get( 'comp' );
$c = new $r();
/** @psalm-trace $c */;
|
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Example from the documentation: https://psalm.dev/docs/annotating_code/type_syntax/utility_types/#tk
https://psalm.dev/r/be7148dbb1
The text was updated successfully, but these errors were encountered: