Skip to content

typescript/sync: tuples and type literals from checker.typeToTypeNode print multi-line (SingleLine flag lost) #64991

Open

Description

🔎 Search Terms

typeToTypeNode multiline, printNode single line, EmitFlags SingleLine api, typescript/sync printer, typeToTypeNode tuple printed on several lines

🕗 Version & Regression Information

⏯ Playground Link

n/a - needs the typescript/sync API

💻 Code

// input.ts
export type Shape = { tuple: [Date, number]; obj: { a: string } };
// repro.mjs - node 24.18, typescript@7.1.0-dev.20261027.1
import path from 'node:path';
import { API } from 'typescript/sync';

const entry = path.resolve('input.ts');
const api = new API({ cwd: process.cwd() });
const program = api.createProgram([entry], { strict: true });
const checker = program.getProject().checker;
const sourceFile = program.getSourceFile(entry);
const alias = sourceFile.statements.find(s => s.name?.text === 'Shape');
const shape = checker.getDeclaredTypeOfSymbol(checker.getSymbolAtLocation(alias.name));
for (const prop of checker.getPropertiesOfType(shape)) {
    const type = checker.getTypeOfSymbol(prop);
    const node = checker.typeToTypeNode(type, sourceFile, 0);
    console.log(`--- ${prop.name}`);
    console.log('printNode:');
    console.log(api.printer.printNode(node));
    console.log(`typeToString: ${checker.typeToString(type)}`);
}
api.close();

🙁 Actual behavior

--- tuple
printNode:
[
    Date,
    number
]
typeToString: [Date, number]
--- obj
printNode:
{
    a: string;
}
typeToString: { a: string; }

the same script on 6.0.2 (ts.createProgram, ts.createPrinter().printNode(ts.EmitHint.Unspecified, node, sourceFile)) prints [Date, number] and { a: string; }

🙂 Expected behavior

printNode gives [Date, number] and { a: string; } on one line, like 6.0 and like typeToString

Additional information about the issue

we generate a big .ts file of request/response types from typeToTypeNode output and are moving the generator from the 6.0 API to typescript/sync. this is the one difference we can't work around - every tuple and inline object type from the checker now takes several lines, and on one of our services ~14.5k of the 16k lines of the generated file change

as far as we can see the node encoder only sends multiLine for blocks and object/array literals (getNodeCommonData in dist/api/node/encoder.generated.js), and PrintNodeOptions only has preserveSourceNewlines, neverAsciiEscape and terminateUnterminatedLiterals, so the client can't ask for single-line. keeping the node builder's single-line flag on these nodes, or a printer option, would both do it for us

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions