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

Exporting of TypeScript types separate from their declarations doesn't get erased #6065

Closed
pelotom opened this issue Aug 8, 2017 · 7 comments · Fixed by #9944
Closed

Exporting of TypeScript types separate from their declarations doesn't get erased #6065

pelotom opened this issue Aug 8, 2017 · 7 comments · Fixed by #9944
Labels
area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Docs 📝 A type of pull request used for our changelog categories

Comments

@pelotom
Copy link

pelotom commented Aug 8, 2017

Hi, I just tried out the new babel-preset-typescript in version 7.0.0-alpha.19 with much anticipation. Unfortunately I discovered that it doesn't erase types that are exported separate from their declaration:

type Foo = number
export { Foo }

gets transformed to

export { Foo }

which of course generates an error if you try to run it: ReferenceError: Foo is not defined. It correctly deletes the export if you write

export type Foo = number
@babel-bot
Copy link
Collaborator

Hey @pelotom! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@pelotom pelotom changed the title Exporting of TypeScript types don't get erased Exporting of TypeScript types doesn't get erased Aug 8, 2017
@pelotom pelotom changed the title Exporting of TypeScript types doesn't get erased Exporting of TypeScript types separate from their definitions does't get erased Aug 8, 2017
@pelotom pelotom changed the title Exporting of TypeScript types separate from their definitions does't get erased Exporting of TypeScript types separate from their declarations does't get erased Aug 8, 2017
@ghost
Copy link

ghost commented Aug 8, 2017

Hi @pelotom, we haven't written the guide for this yet, but this plugin needs to be used with the --isolatedModules option. If you have that enabled (and a recent enough version of the typescript package), you should get a compile error, because we don't support re-exports of types in that mode. The reason being that in general, in order to determine whether something is a type, we need information about other modules.

import { T } from "./other-module";
export { T }; // Is this a type export?

In babel we transpile files independently, so we can't access the information we would need there.

@Jessidhia
Copy link
Member

@andy-ms is something like import { T as ImportedT } from './other-module'; export type T = ImportedT (intended to be) supported?

@ghost
Copy link

ghost commented Aug 9, 2017

Yes, that should work since we can use local information to determine that it's a type-only import.

@hzoo hzoo added PR: Docs 📝 A type of pull request used for our changelog categories and removed i: bug labels Sep 1, 2017
@rodrigopivi
Copy link

Experiencing the same error:

export type IExecuteGQL = <T>({ persistedQueryId, variables }: IGQLPersistedQuery<T>) => Promise<T>;

Valid typescript that does not work with current babel-preset-typescript 7.0.0-beta.3

@ghost
Copy link

ghost commented Oct 24, 2017

Hi @rodrigopivi, see #6667. In the meanwhile you could replace the binding pattern { persistedQueryId, variables } with a single name, e.g. <T>(query: IGQLPersistedQuery<T>) => Promise<T>;

@pelotom pelotom changed the title Exporting of TypeScript types separate from their declarations does't get erased Exporting of TypeScript types separate from their declarations doesn't get erased Dec 19, 2017
@alangpierce
Copy link
Contributor

@andy-ms it seems like the original code should be reasonable to handle even in isolatedModules mode:

type Foo = number
export { Foo }

I realize that isolatedModules currently gives an error, but seems like that error is overly aggressive here. This isn't really a re-export, it's just an exported type across two statements.

Just like how Babel (and TS) will remove imported identifiers that are only used as types in the file, couldn't it also remove exported identifiers that are only declared as types in the file? It looks to me like the TypeScript compiler already does this with transpileModule, so seems like a case where Babel/isolatedModules is more limited than it needs to be.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 16, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Docs 📝 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants