-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Closed
Labels
outdatedA closed issue/PR that is archived due to age. Recommended to make a new issueA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: types
Description
💻
- Would you like to work on a fix?
How are you using Babel?
Programmatic API (babel.transform, babel.parse)
Input code
import parser from "@babel/parser";
const file = await parser.parse("export default class {}", {sourceType: "module"});
const exportDecl = file.program.body[0] as ExportDefaultDeclaration;
const classDecl = exportDecl.declaration as ClassDeclaration;
console.log(classDecl.id.name); // TypeError: Cannot read properties of null (reading 'name')
// TypeScript does not catch this error since the type of `id` is `Identifier` not `Identifier | null`.Configuration file name
No response
Configuration
No response
Current and expected behavior
When a class is a default export, it is not required to have an identifier, so parse correctly outputs a ClassDeclaration with its id property set to null when the identifier is omitted. However, the ClassDeclaration type from @babel/types does not have an optional id property. This results in situations where TypeScript users will be confident that the id of a ClassDeclaration is non-null when it might not be.
The ClassDeclaration interface's id property's type should optional and its type annotation should be Identifier | null.
Environment
- @babel/parser@7.22.14
- @babel/types@7.22.11
Possible solution
The ClassDeclaration interface's id property's type should optional and its type annotation should be Identifier | null.
Additional context
No response
Metadata
Metadata
Assignees
Labels
outdatedA closed issue/PR that is archived due to age. Recommended to make a new issueA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: types