Predicate `value is object` doesn't work in Array.prototype.filter()
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- compilers
Research direction
Start with the linked TypeScript Playground reproduction and compare how the type predicate is applied by Array.prototype.filter() for isNumber versus isObject. Trace the type-checking path for filter predicates, then add coverage showing that filtering the mixed array produces { a: number }[] and run the relevant compiler tests.
Written by the indexing model from the issue text.
Description
Bug Report
🔎 Search Terms
type narrowing, type predicate, value is object, filter(), lodash.isObject
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards.
⏯ Playground Link
Playground link with relevant code
💻 Code
// isNumber and isObject are modeled after the functions by the same names in lodash.
function isNumber(value?: any): value is number {
return typeof value === "number";
}
function isObject(value?: any): value is object {
return typeof value === "object";
}
// Each element could be an object or a number, so we can't treat it as either.
const variousThings = [{ a: 1 }, 1, "abc"];
variousThings[0] + 1;
variousThings[0].a;
// Filtering for only numbers yields values we can add, but not get properties of.
const allNumbers = variousThings.filter(isNumber);
allNumbers[0] + 1;
allNumbers[0].a;
// But filtering for only object yields fails to remove number from the union type.
const allObjects = variousThings.filter(isObject);
allObjects[0] + 1;
allObjects[0].a;
// Yet isObject is able to narrow a single element in a simple conditional.
const value = variousThings[0];
if (isObject(value)) {
value.a
}
🙁 Actual behavior
allObjects was typed as (string | number | { a: number })[], so both lines below const allObjects fail.
🙂 Expected behavior
allObjects should be typed as { a: number }[], so allObjects[0].a should pass the type checker. Notably, as seen above, isObject does manage to narrow the type of a single value with the union type to the only member of the union that's an object, it just can't narrow all of the elements using .filter() the way isNumber is able to.
I can't conceive of why they behave differently, but I also don't know the internals well. I think this difference is undesired, but I might be missing something deeper.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/TypeScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
microsoft/TypeScript#64322 · 2 comments · 1 reaction · 2 assignees ·
-
Possible Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/TypeScript#64278 · 1 comment · 1 reaction ·
-
Docs
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
microsoft/TypeScript#64118 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
microsoft/TypeScript#64094 ·
-
Docs
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
microsoft/TypeScript#63959 · 5 comments ·
All issues in microsoft/TypeScript
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100