Fix IsTypeOf<>() to work with all types and improve array overload resolution #3363
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #[issue_number]
Problem
Two assertion issues were preventing users from upgrading to version 0.70.x:
IsTypeOf<>() only worked with
objecttypes: Attempting to use.IsTypeOf<T>()on non-object types resulted in compilation errors:IsNotNull() with nullable arrays lost type information: Using
.IsNotNull()with nullable byte arrays returnedIEnumerable<byte>instead ofbyte[], preventing access to array-specific members:Solution
IsTypeOf<>() - Fully Fixed ✅
Added a generic overload that works with any
IAssertionSource<TValue>:This allows
.IsTypeOf<T>()to work with all types while maintaining backwards compatibility with the existingobject-specific overload.Before:
After:
Array Overload Resolution - Improved⚠️
Modified the array overload to accept nullable arrays (
TItem[]?instead ofTItem[]), improving overload resolution when nullable arrays are passed toAssert.That().Note: The compile-time return type is still
IEnumerable<byte>due to howCollectionAssertion<TItem>implementsIAssertionSource<IEnumerable<TItem>>. This is an architectural limitation that would require introducing a newArrayAssertion<TItem>class to fully resolve. However, the runtime type is correct (byte[]), so users can cast if needed:(byte[])result.Tests Added
Added 5 comprehensive tests to verify the IsTypeOf fix in
TUnit.Assertions.Tests/TypeOfTests.cs:IsTypeOf_Works_With_NonObject_Type- Verifies the main fix: IsTypeOf works with non-object types using two type parametersIsTypeOf_Works_With_Object_SingleTypeParameter- Verifies backwards compatibility with single type parameter on objectIsTypeOf_Works_With_NullableObject- Tests with nullable object typesIsTypeOf_Fails_When_Type_Mismatch- Ensures proper failure when types don't matchIsTypeOf_TwoTypeParameters_Works_With_AnySourceType- Tests the generic overload with various source types (string, int)Testing
objectFiles Changed
TUnit.Assertions/Extensions/AssertionExtensions.cs- Added genericIsTypeOf<TExpected, TValue>overloadTUnit.Assertions/Extensions/Assert.cs- Modified array overload to accept nullable arraysTUnit.Assertions.Tests/TypeOfTests.cs- Added 5 comprehensive testsMigration Guide
No changes required for existing code. The fixes are backwards compatible and enable previously broken code to compile.
Original prompt
Fixes #3362
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.