question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: -Is operator: suppress "Cannot convert"error

See original GitHub issue

Suppress the error on the -is operator when the type is supplied as a string:

$a -is 'UndefinedType'

Cannot convert the "UndefinedType" value of type "System.String" to type "System.Type".
At line:1 char:1
+ $a -is 'UndefinedType'
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

If “strict” programming is required, the system.type (square brackets) should be used instead:

$a -is [UndefinedType]

Unable to find type [UndefinedType].
At line:1 char:8
+ $a -is [UndefinedType]
+        ~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (UndefinedType:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

(Were the error is actually generated by the type casting and not the -isoperator.)/sub>

This would allow for easier type checking on types that might not exist in certain environments (like [semver] that doesn’t exist in Windows PowerShell, see also: Stackoverflow: How to test a (possible) unknown type?

Besides, I think this is more in line with the general PowerShell behavor set by the PowerShell team: “Type Conversion in PowerShell is, without question, one of the most useful ingredients in its “Magic Sauce” of administrator effectiveness.” and looking to other comparison operators, there aren’t many operators that can produce an error.

In other words, from my perspective: $a -is 'UndefinedType' should just return $False without error and $a -isnot 'UndefinedType' should just return $True without error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

2reactions
BrucePaycommented, Sep 10, 2019

@iRon7 Even in non-strict mode, PowerShell tries to error on impossible behaviour. For example

[int] "abc"

is an error (unlike say Javascript). Likewise 123 + "abc" is an error. $x.nosuchmethod() is an error. 123/0 is an error. [type] 'nosuchtype' is an error. As much as possible, we try to treat things that make no sense as errors. In particular, we wanted implicit (or explicit) type conversions to be rational so [int] "123" works but [int] "abc".

@SeeminglyScience

I think this would mostly end up just making typos harder to find.

Agree 100%. Type names tend to be long so it’s easy to make a mistake.

2reactions
vexx32commented, Sep 9, 2019

Yeah, I think I can get behind this. Asking whether an object is of a nonexistent type should probably just return $false; it certainly still answers the query the operator is designed to address.

FWIW, you can workaround this for the time being with a pattern like this:

$Type = 'UnknownType' -as [type]
$Result = $Type -and $object -is $Type

Which isn’t ideal but it does avoid the issue in the meantime. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test a (possible) unknown type? - powershell
Thanks to vexx32 for this answer: (see also: Feature Request: -Is operator: suppress "Cannot convert"error #10504)
Read more >
After upgrading from Tableau Desktop 2019.4 to 2021.2 ...
I uninstalled both versions of Desktop and the ODBC driver and reinstalled 2021.2 and still get the error when refreshing the database.
Read more >
ShellJS - Unix shell commands for Node.js
Shelljs print stderr to console even if exec-only "silent" is true #905; refactor: remove common.state.tempDir #902; Can't suppress stdout for echo #899 ...
Read more >
RFC 5259: Internet Message Access Protocol - CONVERT ...
Abstract CONVERT defines extensions to IMAP allowing clients to request ... OK - convert completed NO - convert error: can't fetch and/or convert...
Read more >
[Solved]-How to test a (possible) unknown type?-powershell
(see also: Feature Request: -Is operator: suppress "Cannot convert"error #10504) 'UnknownType' -as [type] -and $object -is [UnknownType].
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found