Registry / serialization / babel-type-scopes

babel-type-scopes

JSON →
library1.1.0jsnpmunverified

Utility functions for looking up and working with Flow and TypeScript type scopes in Babel. Version 1.1.0 provides four main functions: isTypeScope, getClosestTypeScopePath, getOwnTypeBindings, and getTypeBinding. These helpers identify whether a Babel path creates a type scope, find the nearest ancestor type scope, retrieve type bindings defined in the current scope, and search for type bindings up the scope chain. The package supports both Flow and TypeScript, and is intended for Babel plugin or tool authors. It is lightweight with no production dependencies. Released in 2018, the library is stable but sees infrequent updates. Similar functionality exists in @babel/traverse, but this package provides more targeted scope inspection.

npm install babel-type-scopes
INSTALL
IMPORT
SIG · BABEL-TYPE-SCOPES
B
babel-type-scopes
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isTypeScope
import { isTypeScope } from 'babel-type-scopes'
const isTypeScope = require('babel-type-scopes').isTypeScope
Correct named import. The package is ESM-only; require will not work without transpilation.
getClosestTypeScopePath
import { getClosestTypeScopePath } from 'babel-type-scopes'
import getClosestTypeScopePath from 'babel-type-scopes'
Named export, not default export.
getTypeBinding
import { getTypeBinding } from 'babel-type-scopes'
const { getTypeBinding } = require('babel-type-scopes')
CommonJS require with destructuring is possible if you use a bundler, but ESM import is recommended.

Parses TypeScript code, traverses AST, and demonstrates each exported function on a type alias.

import { isTypeScope, getClosestTypeScopePath, getOwnTypeBindings, getTypeBinding } from 'babel-type-scopes'; import { parse } from '@babel/parser'; import traverse from '@babel/traverse'; const code = `type Foo = string; let x: Foo = 'hello';`; const ast = parse(code, { plugins: ['typescript'] }); traverse(ast, { TSTypeAliasDeclaration(path) { console.log(isTypeScope(path)); // true const scopePath = getClosestTypeScopePath(path); console.log(scopePath.node.type); // TSTypeAliasDeclaration const bindings = getOwnTypeBindings(scopePath); console.log(Object.keys(bindings)); // ['Foo'] const binding = getTypeBinding(path, 'Foo'); console.log(binding.kind); // 'declaration' } });
Debug
Known issues
gotchaThe package only considers type scopes (type declarations, interface, etc.). Regular variable scopes are not covered.
fix
Use @babel/traverse's getBinding for non-type bindings.
affects: <=1.1.0
gotchagetClosestTypeScopePath may return the current path if it is a type scope, which may be unexpected.
fix
Check if the returned path is the same as the input; if so, the path itself is a type scope.
affects: all
gotchagetTypeBinding searches only type scopes; it does not look up regular bindings. This may cause confusion if a binding is declared with 'let' or 'const'.
fix
Use getTypeBinding only for type-level bindings (types, interfaces). For value bindings, use @babel/traverse's scope.getBinding.
affects: all
Errors
Common errors & fixes
Cannot find module 'babel-type-scopes'
Package not installed or missing from node_modules.
fix
Run 'npm install babel-type-scopes' or 'yarn add babel-type-scopes'.
TypeError: isTypeScope is not a function
Incorrect import syntax (e.g., default import instead of named import).
fix
Use 'import { isTypeScope } from 'babel-type-scopes';'
TypeError: Cannot read property 'kind' of undefined
No type binding found in the scope chain for the given name.
fix
Ensure the name exists as a type binding; consider checking getOwnTypeBindings first.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
babel-type-scopes — npm install babel-type-scopes · libregistry