In TypeScript, you have the option to access a property using either dot notation or bracket notation. The key difference between the two lies within error handling, depending on your compiler settings.
If the noImplictAny
setting is set to false
(which is the default), using bracket notation will not produce an error even if the property cannot be validated at compile time, resulting in an implicit any
type. For example,
ast.program.body[0]["declaration"]
would be inferred as
any
, with TypeScript failing to check any subsequent
.properties
. You can test this behavior in the
playground.
Conversely, setting noImplictAny
to true will prompt TypeScript to flag potential issues where it cannot verify that declaration
indeed exists within ast.program.body[0]
. This will result in errors for both instances, though they may vary slightly. You can experiment with this behavior in the playground.
To ensure safe access of ast.program.body[0]
properties, consider narrowing its type prior to retrieval. Below is an approach demonstrating how to narrow ast.program.body[0]
from a generic statement to an object expression with a defined properties
member:
import { parse } from "@babel/parser";
import { isExpressionStatement, isObjectExpression } from "@babel/types";
const ast = parse('({key: "something"})', {
sourceType: "module",
plugins: ["typescript"],
});
let firstStatement = ast.program.body[0];
if (!isExpressionStatement(firstStatement) || !isObjectExpression(firstStatement.expression)) {
throw Error("Expected statement not found");
}
console.log(firstStatement.expression.properties);
Explore further in the TypeScript playground.
Please note that I modified {key: "something"}
to ({key: "something"})
assuming you intended to parse an object literal. Your original version resembled a block statement with a labeled statement inside, distinguishable here: Block statement vs Object literal.