{"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,"search_vec":"'1.1.5':25 '2020':32 'altern':35 'approach':23 'babel':2,6,57,73 'babel-plugin':72 'babel-plugin-ts-optchain':1 'call':53 'chain':11,41,70 'convert':50 'depend':61 'e.g':13 'environ':43 'express':12 'javascript':71 'latest':26 'nativ':39,68 'new':65 'obj':14 'optchain':5,22 'option':10,40,69 'plugin':3,7,74 'prefer':67 'project':66 'prop':15 'provid':33 'recommend':63 'sinc':31 'stabl':28 'support':47 'transform':9 'ts':4,21 'ts-optchain':20 'tsc':59 'typescript':17,37,75 'unmaintain':30 'use':18 'version':24 'work':54 'wrap':52","created_at":"2026-04-25T11:16:43.610444+00:00","updated_at":"2026-04-25T11:16:43.610444+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/epeli/babel-plugin-ts-optchain","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/babel-plugin-ts-optchain","openapi_spec":null,"status_page":null,"smithery":null,"categories":["type-stubs"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}