telescope
telescope copied to clipboard
import "type" keyword for compatibility with TypeScript verbatimModuleSyntax
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);