Predicate `value is object` doesn't work in Array.prototype.filter()

Open
#44,777 3 comments 2 reactions 0 assignees View on GitHub

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

Awaiting More Feedback Suggestion

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/TypeScript

All issues in microsoft/TypeScript

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.