Open jsiefer opened 4 years ago
I think the syntax would have to be something like
/**
* @psalm-type array-like<TKey as array-key, TValue>
* = array<TKey, TValue>|ArrayAccess<TKey, TValue>&iterable<TKey, TValue>
*/
I can definitely see the value, albeit in a few very specific settings
I am just evaluating psalm for our codebase and that is a scenario that occurs quite often. We allow basic structures using array, but in some impl. we use "array-like" impl. for lazy loading purpose.
Would be nice if you could create your own types. But a simple array-like pseudo type would already work for many cases.
I tried making a plugin for this the other night, but could not quite get my head around completely. Is there a chance you could point me in the right direction what hook to use for hat?
@jsiefer there isn't a plugin hook for the type-expander or type parser to register new types.
It's tangential to the main issue, but I like the idea enough that I've just added support (in the next major version, coming probably later this month) for a shortcut
arraylike-object<K,V> => Traversable<K,V>&ArrayAccess<K,V>
See it here: https://psalm.dev/r/ce6ed6785e
This means you could express your types as array<K,V>|arraylike-object<K,V>
, saving a bit of typing.
I found these snippets:
@muglug Cool, thanks! In multi-dimension arrays this becomes quite difficult to type out otherwise ;)
I have another use-case for templating inside aliases: https://psalm.dev/r/e8a0c150cf
I found these snippets:
It would be nice if one could uses templates in type alias. Is that doable? Or is there something similar to solves bellow problem?
https://psalm.dev/r/a357fb65ab