{"id":19231,"library":"babel-plugin-ts-optchain","title":"babel-plugin-ts-optchain","description":"Babel plugin that transforms optional chaining expressions (e.g., `obj?.prop`) for TypeScript using the ts-optchain approach. Version 1.1.5 (latest) is stable but unmaintained since 2020. Provides an alternative to TypeScript's native optional chaining for environments that don't support it, by converting `?.` into `_` wrapped calls. Works only with Babel, not tsc. No dependencies. Not recommended for new projects; prefer native optional chaining.","status":"maintenance","version":"1.1.5","language":"javascript","source_language":"en","source_url":"https://github.com/epeli/babel-plugin-ts-optchain","tags":["javascript","babel-plugin","typescript"],"install":[{"cmd":"npm install babel-plugin-ts-optchain","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-ts-optchain","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-ts-optchain","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a Babel plugin, not meant to be imported directly. Add it to .babelrc or babel.config.js as a string.","wrong":"import plugin from 'babel-plugin-ts-optchain';","symbol":"plugin","correct":"module.exports = { plugins: ['ts-optchain'] };"},{"note":"The package has no default export; it's a Babel plugin referenced by name.","wrong":"import babelPluginTsOptchain from 'babel-plugin-ts-optchain';","symbol":"default","correct":"module.exports = { plugins: ['ts-optchain'] };"},{"note":"The companion 'ts-optchain' package provides the runtime `oc` function. Ensure both packages are installed: npm install ts-optchain babel-plugin-ts-optchain","wrong":"import oc from 'ts-optchain';","symbol":"ts-optchain types","correct":"import { oc } from 'ts-optchain';"}],"quickstart":{"code":"// Install: npm install --save-dev babel-plugin-ts-optchain ts-optchain\n// .babelrc\n{\n  \"plugins\": [\"ts-optchain\"]\n}\n// Example TypeScript file\nimport { oc } from 'ts-optchain';\nconst obj = { a: { b: 'hello' } };\nconst val = oc(obj).a.b(); // returns 'hello'\nconst missing = oc(obj).x.y('default'); // returns 'default'\n// Compiled output uses the oc function\nconsole.log(val, missing);","lang":"typescript","description":"Shows installation, Babel config, and usage with ts-optchain's oc function for safe optional access."},"warnings":[{"fix":"Migrate to native optional chaining and remove babel-plugin-ts-optchain.","message":"Package is effectively unmaintained since 2020. Use TypeScript 3.7+ native optional chaining (obj?.prop) when possible.","severity":"deprecated","affected_versions":">=1.0"},{"fix":"Run: npm install ts-optchain babel-plugin-ts-optchain","message":"Requires both 'babel-plugin-ts-optchain' and 'ts-optchain' packages to be installed. Missing one causes runtime errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Remove the plugin and use TypeScript's native ?. operator.","message":"Babel plugin no longer necessary for TypeScript 3.7+. Native optional chaining is compile-time and more efficient.","severity":"deprecated","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure babel.config.js includes the ts-optchain plugin and that you are using the correct Babel parser. Example: { plugins: ['ts-optchain'] }","cause":"Babel is not set up to parse modern optional chaining syntax.","error":"SyntaxError: Unexpected token '.'"},{"fix":"Install the package: npm install ts-optchain","cause":"Missing runtime dependency 'ts-optchain'.","error":"Cannot find module 'ts-optchain'"},{"fix":"Invoke the last property access as a function: oc(obj).a.b()","cause":"Using oc() without calling the final property as a function (e.g., oc(obj).a.b instead of oc(obj).a.b()).","error":"TypeError: oc(...).a.b is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}