Powershell: -as operator with [type] quietly ignores extraneous suffixes in full type names

Created on 24 Sep 2019  路  2Comments  路  Source: PowerShell/PowerShell

Note: Even though * and ? are reminiscent of wildcard patterns, -as does _not_ generally support them; the problem is limited to appending * or ? to the _full_ type name.

Steps to reproduce

# These work as expected.
'System.Int32' -as [type] | should -be ([int])
'System.Int3*' -as [type] | should -not -be ([int])  # incomplete name with *, not supported
# These don't - they unexpectedly still return [int]
'System.Int32*' -as [type] | should -not -be ([int]) # complete name with *
'System.Int32?' -as [type] | should -not -be ([int]) # complete name with ?

_Update_: The problem is more general - see @HumanEquivalentUnit's comment below.

Expected behavior

All tests should pass.

Actual behavior

The last two tests fail, because the trailing * / ? following the full type name is unexpectedly ignored:

Expected [int] to be different from the actual value, but got the same value.

Environment data

PowerShell Core 7.0.0-preview.4
Issue-Bug WG-Engine

Most helpful comment

This doesn't seem restricted to just *? characters, or to just one character, e.g.

PS C:\> 'system.int32%fwefwef' -as [type]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

Quick testing, I guess any symbol which stops name parsing leads to anything in the string after that being igored.

All 2 comments

This doesn't seem restricted to just *? characters, or to just one character, e.g.

PS C:\> 'system.int32%fwefwef' -as [type]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

Quick testing, I guess any symbol which stops name parsing leads to anything in the string after that being igored.

Thanks, @HumanEquivalentUnit - great sleuthing - I've updated the issue title and added a link to your comment in the OP.

Was this page helpful?
0 / 5 - 0 ratings