telescope icon indicating copy to clipboard operation
telescope copied to clipboard

import "type" keyword for compatibility with TypeScript verbatimModuleSyntax

Open turadg opened this issue 1 year ago • 0 comments

Problem

In the generated code, the import statements don't distinguish between runtime values and buildtime types. E.g.

import { Lien, LienSDKType } from './lien.js';
import { JsonSafe } from '../../json-safe.js';

This causes errors with TypeScript verbatimModuleSyntax and libraries like https://bloomberg.github.io/ts-blank-space/ that require it.

Proposed solution

In the output precede type imports with type. So for the example above,

import { Lien, type LienSDKType } from './lien.js';
import { BinaryReader, BinaryWriter } from '../../binary.js';
import { type JsonSafe } from '../../json-safe.js';

Fallback

This snippet can be used with child_process exec to clean up the files after they're generated:

  const command = `
    find ${fullPath} -type f -exec sed -i '' \
    -e 's/import { JsonSafe/import {type JsonSafe/g' \
    -e 's/\\([{,]\\) \\([[:alnum:]_]*SDKType\\)/\\1 type \\2/g' {} +
  `;
  exec(command);

turadg avatar Oct 22 '24 16:10 turadg