Closed
Description
π Search Terms
export type invalid JavaScript
π Version & Regression Information
- This changed between versions 5.1 and 5.2
β― Playground Link
π» Code
import type { SomeType } from './type';
export { SomeType };
π Actual behavior
TypeScript will generate the following JS:
export { SomeType };
This JS is invalid since SomeType
is not defined. Basically the import type
is removed, but the export
of the type was not.
π Expected behavior
Type should be removed from export as in 5.1
Additional information about the issue
In the example above the source code has errors, so it could be argued that the output does not have to be valid. The issue was however discovered when using transpileModule
. transpileModule
similarly can't by design resolve the types and will preserve them in the output even though the types actually exist.
This is going to block us at Bloomberg from fully adopting TS 5.2 as we have workflows that depend on transpileModule
Activity
spyhere commentedon Nov 17, 2023
This is not a solution, but I would suggest using
export {} from ''
signature next time.fatcerberus commentedon Nov 17, 2023
Hm, this seems tricky. I recall someone (maybe @ahejlsberg?) saying that
import type { foo }
actually imports both the type and value meanings of a symbol, and you just can't use it in emitting positions. This is so that you can still write e.g.typeof foo
. So arguably, theexport
at the bottom should be an error, at least underisolatedModules
, becauseSomeType
may also have a value meaning but TS can't know that without resolving the import.dragomirtitian commentedon Nov 17, 2023
Sure, there are work arounds. The best one IMO (which we are working towards) is to use
verbatimModuleSyntax
which would flag this as an error, however we are not ready to enable that at present.dragomirtitian commentedon Nov 17, 2023
I mean it would probably be an error under
verbatimModuleSyntax
, but under our settings it does not produce any error and also generates invalid JS which is not ok.fatcerberus commentedon Nov 17, 2023
@dragomirtitian Yeah, I didn't mean to imply it wasn't problematic; it definitely is :) It just seemed tricky since
import type
also pulls in the value-space meaning which means the compiler must be able to resolve the import to know whether it should elide the re-export or simply error on it.This should definitely error under isolatedModules and I consider it a bug that it doesn't.
jakebailey commentedon Nov 17, 2023
Bisects to #54799. @andrewbranch
andrewbranch commentedon Nov 17, 2023
Itβs not a missing
isolatedModules
error; itβs just an emit bug. This code is correctly transpilable without knowing how/whether'./type'
resolves.fatcerberus commentedon Nov 17, 2023
Huh, it seemed weird to me that one could
import type { Class } from "foo"
and then dotypeof Class
but still have the re-export elided (without being an error).