TypeScript
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."
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.
The error is exactly describing the situation that's happening.
You can write
import type { x } from './foo';
const x = 7
export { x };
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?
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 See https://github.com/microsoft/TypeScript/issues/40583#issuecomment-693542271
Thanks! This works indeed:
type x = import("./foo").x;
export const x = 7;