Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated Database.ts is not compatible with TS 5.0 option verbatimModuleSyntax #436

Open
thelinuxlich opened this issue Jul 18, 2023 · 4 comments

Comments

@thelinuxlich
Copy link
Contributor

thelinuxlich commented Jul 18, 2023

Since TS 5.0 there is a new option called verbatimModuleSyntax which frameworks like SvelteKit set as true by default.

When this option is true, export default Database gives the following TS error:

An 'export default' must reference a value when 'verbatimModuleSyntax' is enabled, but 'Database' only refers to a type.

The solution is replacing this line:

// old
export default Database;
// new
export type { Database as default };

Workaround in .kanelrc.js

postRenderHooks: [
  (p, lines) => {
	if (p.endsWith("Database.ts")) {
		lines.pop();
		return [...lines, "export type { Database as default };"];
	}
	return lines;
  },
]
@kristiandupont
Copy link
Owner

Right, this is all something that I can't keep pushing much longer. I'll try to get around to looking into it soon!

@seivan
Copy link

seivan commented Sep 25, 2023

There's another issue with allowImportingTsExtensions, lemme know if you want it in a separate ticket & pr.
The solutions are essentially the same, with allowing (or reading tsconfig) to understand what to do.

Current hack is

      postRenderHooks: [(_path, lines) => {
        return lines.map(l => l.includes("'./") ? l.replace("\';", '.ts\';') : l)
      }],

@alecmev
Copy link
Contributor

alecmev commented May 28, 2024

import type { ... } is also preferred over import { type ... }, as it results in better elision (the import statements are completely removed, instead of being left behind as side effects). This is now fixed.

The above + ESM-friendly .js, this is the hook:

const verbatimModuleSyntaxHook: PostRenderHook = (_, lines) =>
  lines.map((line) => {
    let x = line;

    if (x.startsWith('import ')) {
      if (x.includes('default as') && !x.includes(', ')) {
        x = x.replace('{ default as ', '').replace('} ', '');
      }
    } else if (
      x.startsWith('export default') &&
      !x.startsWith('export default interface')
    ) {
      x = x
        .replace('export default', 'export type {')
        .replace(';', ' as default };');
    }

    if (x.includes("from './")) x = x.replace("';", ".js';");

    return x;
  });

@kristiandupont
Copy link
Owner

To subscribers here, the new importsExtension config field might be helpful. There is still some work to be done though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants