Skip to content

Support automatically determining if a string literal should be written for a property key or value in Writers.object #1160

@whalemare

Description

@whalemare

Describe the bug

Version: 10.1.0

I want to generate .ts file with variable content, that depend on my existedObject
Writers generate incorrect object representation

To Reproduce

import { Project, VariableDeclarationKind, Writers } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("test.ts", ``);

const existedObject = {
      id: "some-id",
      "quote-key": 2
}

sourceFile.addVariableStatement({
  isExported: true,
  declarationKind: VariableDeclarationKind.Const,
  declarations: [{
    name: 'flavor',
    initializer: Writers.object(existedObject),
  }]
})

console.log(sourceFile.getFullText())

Output:

export const flavor = { 
        id: some-id, 
        quote-key: 2 
    }; 

Expected behavior

export const flavor = { 
        id: "some-id", 
        "quote-key": 2 
    }; 

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions