TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

TS:1379 An import alias cannot reference a declaration that was exported using 'export type'

Open EliLichtblau opened this issue 2 years ago • 11 comments

Bug Report

🔎 Search Terms

TS1379 An import alias cannot reference a declaration that was exported using 'export type'

🕗 Version & Regression Information

This has always been the behavior as far as I can tell (edit: tested on all versions listed in playground back to >3.7.5 (changed to export type {...} statement when needed))

⏯ Playground Link

Multi file: repro but I will show in playground.

Below are pictures that might be easier to parse

💻 Code

When annotated with type it errors Screen Shot 2023-07-01 at 10 29 21 AM

Screen Shot 2023-07-01 at 10 30 29 AM

When not annotated with type it doesn't error: Screen Shot 2023-07-01 at 10 31 27 AM

Screen Shot 2023-07-01 at 10 31 41 AM

🙁 Actual behavior

Exports of types annotated with type error while those not annotated as types do not error even though TS understand it is still a type.

🙂 Expected behavior

Honestly I don't know which of these behaviors is correct - this behavior could be completely intentional as well - intuitively I think these should match and I prefer the behavior with no errors but it is unclear to me if this is an emit issue? I'm sorry if this behavior is on purpose and I have wasted your time. Really appreciate the work!

EliLichtblau avatar Jul 01 '23 14:07 EliLichtblau

It should be like:

//exporter.ts

type foo = {
  thing: number;
}

export type foo

This should work expectedly. Otherwise, this is intentional. No worries!

gooddavvy avatar Jul 01 '23 14:07 gooddavvy

I'm not sure I understand what you mean?

export type foo = {thing: number} 

works I agree.

//exporter.ts

type foo = {
  thing: number;
}

export type foo

is a syntax error no? I just don't understand the behavior. It's a very specific function in the checker

the error reporter: Screen Shot 2023-07-01 at 10 53 59 AM

the comment around the function that really does the check Screen Shot 2023-07-01 at 10 52 51 AM

I don't exactly understand why this is. If you don't have the time/think its to in the weeds to explain I totally understand! But I'm pretty curious as to why this is the behavior. I only encountered this corner because I wrote a little script to tag all of exports of types as typeOnly=true. (It's my understanding that this is good practice when ts_loader is set to transpileOnly)

EliLichtblau avatar Jul 01 '23 14:07 EliLichtblau

There was a missing semicolon. You can't put type in curly brackets, I believe.

Here might be the issue(s):

  1. TypeScript version: It's worth noting that TypeScript evolves over time, and there may be differences in behavior between different versions. It's possible that the behavior you're observing is specific to the version you're using.

  2. Configuration or compiler options: The behavior of TypeScript can also be influenced by various compiler options and configuration settings. It's worth double-checking your project's TypeScript configuration files (such as tsconfig.json) to see if any specific settings might be affecting this behavior.

  3. Bug or regression: While you mentioned that this behavior has always been consistent, it's still possible that there is an underlying bug or regression in TypeScript that causes this inconsistency. In such cases, it might be helpful to consult the TypeScript documentation, release notes, or community forums to see if there are any known issues related to import aliases and export type.

gooddavvy avatar Jul 01 '23 15:07 gooddavvy

You can put type in curly brackets, it sets the boolean isTypeOnly in ExportSpecifier in the AST, the behavior is the same if isTypeOnly is set in an ExportDeclaration. I can check every version of TS, but this behavior is very specific to export declarations themselves (not statements branded with export) tied to import equals declarations - notably this is of course fine in non-importEqualsDeclarations import statements.

EliLichtblau avatar Jul 01 '23 15:07 EliLichtblau

Yea so I tested back to 3.7.5 (going through the list playground versions locally) where it was a syntax error on my export type statement - This has always been the behavior (at least on some tsconfig setting, I don't know what I would change around to check).

EliLichtblau avatar Jul 01 '23 15:07 EliLichtblau

Not sure how to help. Is AI service available in your current country?

And what about your tsconfig.json?

gooddavvy avatar Jul 01 '23 15:07 gooddavvy

What causes you to keep disliking my answers, @guillaumebrunerie?

gooddavvy avatar Jul 01 '23 22:07 gooddavvy

@gooddavvy They look AI-generated and particularly unhelpful.

guillaumebrunerie avatar Jul 01 '23 23:07 guillaumebrunerie

The behavior observed in TypeScript, where exports of type-annotated types result in errors while un-annotated ones do not, is unclear whether this is intentional or a bug/limitation. This behavior has been consistent across different versions of TypeScript, including up to version 3.7.5.

Mateusnasciment avatar Jul 02 '23 10:07 Mateusnasciment

1 answer looks AI-generated. Just 1 @guillaumebrunerie

gooddavvy avatar Jul 02 '23 14:07 gooddavvy

Since you have imported * as core, just make a normal definition type thing = core.foo;

zimtsui avatar Mar 10 '25 05:03 zimtsui