Fix ko.unwrap for find/findLast/at(-1) results in LINQ JS translation (FirstOrDefault/LastOrDefault with predicate) - #2106
Draft
tomasherceg with Copilot wants to merge 2 commits into
Draft
Fix ko.unwrap for find/findLast/at(-1) results in LINQ JS translation (FirstOrDefault/LastOrDefault with predicate)#2106tomasherceg with Copilot wants to merge 2 commits into
tomasherceg with Copilot wants to merge 2 commits into
Conversation
…irstOrDefault/LastOrDefault with predicate) Co-authored-by: tomasherceg <5599524+tomasherceg@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix FirstOrDefault and Where predicates in JS translation
Fix ko.unwrap for find/findLast/at(-1) results in LINQ JS translation (FirstOrDefault/LastOrDefault with predicate)
Aug 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Regression in v5:
FirstOrDefault(pred),LastOrDefault(), andLastOrDefault(pred)JS translations generated a hard observable callexpr()on the returned array item. When array items are plain mapped objects (e.g. control property arrays), calling the item as a function throws at runtime.Root cause
firstOrDefaultPred,lastOrDefault, andlastOrDefaultPredtranslators inJavascriptTranslatableMethodCollection.csannotated their result withVMPropertyInfoAnnotationwithoutisObservable: false. This causedJsViewModelPropertyAdjusterto setResultIsObservableAnnotation, which then causedKnockoutObservableHandlingVisitorto emit a hardexpr()unwrap — correct only when items are guaranteed to be ko-observable functions, but not when they're plain objects.Before:
After:
Changes
firstOrDefaultPred(find): addisObservable: false+ResultMayBeObservableAnnotation→ emitsko.unwrap(find(pred))instead offind(pred)()lastOrDefault(at(-1)): same pattern →ko.unwrap(at(-1))instead ofat(-1)()lastOrDefaultPred(findLast): same pattern →ko.unwrap(findLast(pred))instead offindLast(pred)()ko.unwrapis already used for lambda parameters inside predicates (e.g.ko.unwrap(f)insidefind), making this consistent. For actual ko-observablesko.unwrap(x) === x(), so the behavior is equivalent; for plain objects it returns the object itself rather than throwing.