TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

"import type { X }" with exported value with same name gives "Individual declarations in merged declaration 'X' must be all exported or all local."

Open mb21 opened this issue 3 years ago • 4 comments

Bug Report

🔎 Search Terms

  • Individual declarations in merged declaration 'X' must be all exported or all local.
  • import type

🕗 Version & Regression Information

  • This is the behavior in every version I tried, the latest being 4.8.3, and I reviewed the FAQ for entries.

⏯ Playground Link

cannot import other files in playground

💻 Code

import type { x } from './foo';

export const x = 7

🙁 Actual behavior

Running tsc gives:

Individual declarations in merged declaration 'x' must be all exported or all local.

🙂 Expected behavior

No error.

mb21 avatar Sep 21 '22 13:09 mb21

The error is exactly describing the situation that's happening.

You can write

import type { x } from './foo';

const x = 7

export { x };

RyanCavanaugh avatar Sep 21 '22 23:09 RyanCavanaugh

Thanks, that works indeed!

I suppose I don't know enough about JavaScript module syntax to see the difference :P export { x } is not a declaration?

mb21 avatar Sep 22 '22 07:09 mb21

Actually, it doesn't work. There is no type error in the exporting file, but when I save what you posted to bar.ts, and in another file:

import { x } from './bar';
console.log(x);

I get:

'x' cannot be used as a value because it was imported using 'import type'

Default export seems to work, but that wouldn't be tree-shakable :-(

mb21 avatar Sep 22 '22 08:09 mb21

@mb21 See https://github.com/microsoft/TypeScript/issues/40583#issuecomment-693542271

whzx5byb avatar Sep 22 '22 14:09 whzx5byb

Thanks! This works indeed:

type x = import("./foo").x;
export const x = 7;

mb21 avatar Sep 23 '22 07:09 mb21