Babel Plugin for idx Utility

3.0.3 · deprecated · verified Tue Apr 21

babel-plugin-idx is a Babel plugin designed to transform usages of the `idx` utility function into explicit null-checking code. The `idx` utility, now deprecated and unmaintained, was created by Facebook to safely access deeply nested properties on objects and arrays where intermediate properties might be `null` or `undefined`, preventing `TypeError` exceptions. The plugin effectively replaces calls like `idx(props, _ => _.user.friends[0].friends)` with verbose but safe conditional expressions, optimizing performance by removing the need for a runtime `idx` function. The current stable version is 3.0.3, but the module is no longer receiving updates. Its primary differentiator was providing safe property access similar to the now-standard optional chaining (`?.`) operator, though with a key difference: `idx` returns `null` or `undefined` for intermediate `null`/`undefined` values, while optional chaining consistently resolves to `undefined`. This plugin is essential for `idx` to function correctly and efficiently, as the `idx` runtime function is purely illustrative.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `idx` function for safe, deeply nested property access on potentially null or undefined objects, including examples of its behavior with different input scenarios, assuming `babel-plugin-idx` is configured. It also highlights the type-safety provided by TypeScript.

import idx from 'idx';

// Define a type for demonstration, often in a .d.ts or .ts file
type User = {
  user: {
    name: string,
    friends?: Array<User>, // Make friends optional
  } | null | undefined, // Make user itself nullable/undefined
};

const props: User = {
  user: {
    name: 'Alice',
    friends: [
      { user: { name: 'Bob', friends: [{ user: { name: 'Charlie' } }] } },
      { user: { name: 'David' } }
    ]
  }
};

const propsWithNullUser: User = { user: null };
const propsWithUndefinedFriends: User = { user: { name: 'Eve', friends: undefined } };

// To run this code, ensure 'babel-plugin-idx' is configured in your Babel setup:
// // babel.config.js
// module.exports = {
//   plugins: [['babel-plugin-idx']],
// };

// Accessing a deeply nested property
const charlieName = idx(props, _ => _.user.friends[0].friends[0].user.name);
console.log('Charlie\'s name:', charlieName); // Expected output: Charlie

// Accessing a non-existent intermediate property
const nonExistentFriendOfFriend = idx(props, _ => _.user.friends[1].friends[0].user.name);
console.log('Non-existent friend of friend:', nonExistentFriendOfFriend); // Expected output: undefined

// Accessing property on a null intermediate
const nameFromNullUser = idx(propsWithNullUser, _ => _.user.name);
console.log('Name from null user:', nameFromNullUser); // Expected output: null

// Accessing property on an undefined intermediate
const nameFromUndefinedFriends = idx(propsWithUndefinedFriends, _ => _.user.friends[0]?.user.name);
console.log('Name from undefined friends:', nameFromUndefinedFriends); // Expected output: undefined

view raw JSON →