Skip to content

Make object type indexableΒ #63399

@dupasj

Description

@dupasj

πŸ” Search Terms

"object record"
"object index"
"record index"
"property access"
"object unknown"
"guard object"

βœ… Viability Checklist

⭐ Suggestion

Explanation of the solving problem

When working with unknown, a very common runtime pattern to ensure an object-like structure is:

obj !== null && typeof obj === 'object'

However, TypeScript currently narrows this to object, which is not indexable and then prevents any property access.
(I checked with noUncheckedIndexedAccess enabled)

Actual workaround

The workaround that actually working for now:

const isRecord = (obj: unknown): obj is Record<string,unknown> => {
  return obj !== null && typeof obj === 'object'
}

const processUnknownObjectWithAssertion = (obj: unknown) => {
  if (!isRecord(obj)) return;
  obj.test; // Works because obj is Record<string,unknown>, then obj.test is unknown
}

Feature request

This leads to extra type check when it's sometime not expected. Would be nice to add an options to type obj !== null && typeof obj === 'object' as Record<string,unknown>.

Resource

Feel free to test the see the snippet here.

πŸ“ƒ Motivating Example

Current behavior:

const processUnknownObject = (obj: unknown) => {
  if (obj === null) return;
  if (typeof obj !== 'object') return;

  obj.test;  ❌ Error: Property 'test' does not exist on type 'object'
}

Expected behavior:

const processUnknownObject = (obj: unknown) => {
  if (obj === null) return;
  if (typeof obj !== 'object') return;

  obj.test; // Works because obj is Record<string,unknown>, then obj.test is unknown
}

πŸ’» Use Cases

  1. What do you want to use this for?
    Make the use of TS easier and more configurable.

  2. What shortcomings exist with current approaches?
    Add an option in the tsconfig to guard obj !== null && typeof obj === 'object' as Record<string,unknown>.

  3. What workarounds are you using in the meantime?
    Manually use a type assertion function:

const isRecord = (obj: unknown): obj is Record<string,unknown> => {
  return obj !== null && typeof obj === 'object'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions