Skip to main content

Command Palette

Search for a command to run...

TS1380: An import alias cannot reference a declaration that was imported using 'import type'

TS1380: An import alias cannot reference a declaration that was imported using 'import type'

Published
4 min readView as Markdown

TS1380: An import alias cannot reference a declaration that was imported using 'import type'

TypeScript is a powerful programming language that builds on JavaScript by adding static types. In simpler terms, TypeScript allows developers to specify the types of variables, function parameters, and return values, which helps catch errors at compile time rather than runtime. This leads to more robust and maintainable code, helping developers avoid common pitfalls that can occur in a dynamically typed language like JavaScript.

Types in TypeScript refer to the various data types that can exist in programming, such as numbers, strings, booleans, arrays, and objects. By defining types, developers can ensure that variables hold the intended data types and that functions receive the correct arguments.

If you want to dive deeper into learning TypeScript or explore AI tools like gpteach to help you learn how to code, be sure to subscribe/follow/join my blog for more insightful content.

What is a Superset Language?

A superset language is a programming language that extends another language by adding new features or enhancements while maintaining compatibility with the original language. TypeScript is a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. This allows JavaScript developers to gradually adopt TypeScript without having to rewrite their existing code.

TypeScript provides features like interfaces, enums, and strict type checking, which are not present in JavaScript but are advantageous for larger, more complex applications.


Understanding TS1380: An import alias cannot reference a declaration that was imported using 'import type'.

In TypeScript, when you import a declaration using import type, you bring in only the type information. This means the imported declaration cannot be used in a way that results in a runtime value, which can lead to issues when trying to create aliases for those imported types.

The error message TS1380: An import alias cannot reference a declaration that was imported using 'import type' signifies this limitation. Let's look at some examples to clarify the situation.

Example That Causes the Error

// types.ts
export type User = {
  name: string;
  age: number;
};

// main.ts
import type { User } from './types'; // Importing as a type

type UserAlias = User; // This line will cause TS1380 error

In this example, we are importing User using import type and then trying to create an alias UserAlias for it. Here, TypeScript raises an error because User was imported only as a type.

How to Fix It

To resolve the TS1380: An import alias cannot reference a declaration that was imported using 'import type', you can import the type without the type keyword, like so:

// main.ts
import { User } from './types'; // Proper import

type UserAlias = User; // Now this is valid

Alternatively, if you want to keep the import type usage for some reason and still create an alias, you can do the following:

// main.ts
import type { User } from './types';

interface UserInterface extends User {} // Use interface for aliasing

Important to Know!

  1. Types vs. Interfaces: Types can define a wide range of structures, while interfaces are specifically for object shapes and can be extended. Both can be used to define complex types.
  2. Type Imports: When using import type, always remember that they are strictly for type information. These imports do not exist at runtime, which is crucial for correct aliasing.

FAQs

  • What happens if I try to reference a type declaration imported with import type? You will receive the TS1380 error because TypeScript enforces separation between runtime and type-only imports.

  • Can I use import type with functions? No, import type should only be used for types (e.g., interfaces, type aliases), not for function or variable imports that need runtime resolution.

  • Why is TypeScript important? TypeScript helps prevent bugs by catching type-related errors during development, improving code quality and maintainability.


Understanding the error TS1380: An import alias cannot reference a declaration that was imported using 'import type' is crucial for TypeScript developers. By adhering to proper importing practices and utilizing structures like interfaces for extending types, you can effectively manage your TypeScript codebase while avoiding common pitfalls. Make sure to consider these principles as you continue to explore TypeScript and its powerful type system!