How to use the createObjectLiteral function from typescript

Find comprehensive JavaScript typescript.createObjectLiteral code examples handpicked from public code repositorys.

typescript.createObjectLiteral creates a new object literal node in a TypeScript AST.

48
49
50
51
52
53
54
55
56
57
    const firstNode = ast_helpers_1.getFirstNode(sourceFile);
    if (firstNode) {
        ops.push(new interfaces_1.AddNodeOperation(sourceFile, firstNode, newImport));
    }
});
const lazyModuleObjectLiteral = ts.createObjectLiteral(modules.map((mod, idx) => {
    let [modulePath, moduleName] = mod.loadChildrenString.split('#');
    if (modulePath.match(/\.ngfactory/)) {
        modulePath = modulePath.replace('.ngfactory', '');
        moduleName = moduleName.replace('NgFactory', '');
fork icon0
star icon0
watch icon1

277
278
279
280
281
282
283
284
285
286
const expression = iife.expression.expression;
const updatedFunction = ts.updateFunctionExpression(expression, expression.modifiers, expression.asteriskToken, expression.name, expression.typeParameters, expression.parameters, expression.type, ts.updateBlock(expression.body, [
    ...expression.body.statements,
    ts.createReturn(expression.parameters[0].name),
]));
let arg = ts.createObjectLiteral();
if (exportAssignment) {
    arg = ts.createBinary(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
}
const updatedIife = ts.updateCall(iife, ts.updateParen(iife.expression, updatedFunction), iife.typeArguments, [arg]);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

How does typescript.createObjectLiteral work?

typescript.createObjectLiteral is a method provided by the TypeScript compiler API that creates an AST node representing an object literal expression, which is an object that has a set of properties with values.

The method accepts an array of ts.ObjectLiteralElementLike objects that can be either ts.PropertyAssignment for key-value pairs or ts.ShorthandPropertyAssignment for properties with the same name as their variable name, and can optionally accept a boolean flag indicating whether the object literal expression is multiline or not.

The returned AST node can then be used for further program analysis and modification by the TypeScript compiler API.

Ai Example

1
2
3
4
5
6
7
import * as ts from "typescript";

// Create an object literal
const objLiteral = ts.createObjectLiteral([
  ts.createPropertyAssignment("name", ts.createStringLiteral("John")),
  ts.createPropertyAssignment("age", ts.createNumericLiteral("30")),
]);

This would create an object literal with two properties: name and age, both with their respective values.

Other functions in typescript

Sorted by popularity

function icon

typescript.SyntaxKind is the most popular function in typescript (82777 examples)