The OutputType
attribute is supposed to give type information through intellisense. However it does not work as expected.
I have tested this in both PSReadline and PowerShell ISE and they work the same.
The following is a sample function I am using:
Function Get-FirstChar
{
[OutputType([String])]
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)][string[]]$Strings
)
process {
foreach ($str in $Strings) {
$str.SubString(0, 1);
}
}
}
When I do:
"John","Simon" | Get-FirstChar | % { $_.<TAB> }
I get the suggestions (regardless of platform):
Equals GetHashCode GetType ToString
However when I do:
("John","Simon" | Get-FirstChar).<TAB>
Then I get all the string methods like SubString
etc.
I have also tried a string array String[]
as the output type and it still don't work :(
Can someone shed like on how to use OutputType
attribute to say that one or more strings will be returned from a powershell function?
Thank you
Obviously, your expectations are correct. I must say I'm surprised it doesn't work for
[string]
as it does work for other, complex types:When I've tried with
[string]
I get only property (and that is not very helpful for strings, the only property they have isLength
). I would consider that a bug, or limitation of the way tools like PowerShell ISE and PSReadline respond to information that objects returned from your functions are strings. E.g. if you try the same with other simple types, result matches the expectations:It seems to be affecting cmdlets too, I can't get any of existing cmdlets that define same
OutputType
to provide tab-completion for string's methods:I guess in either case it's worth reporting on PowerShell's UserVoice.