Skip to content

Missing --isolatedModules error for type referenced by decorator metadata #42281

@janwidmer

Description

@janwidmer

Bug Report

Typescript is not throughing an error for Type imports not having the keyword import type from Files containing only type exports, when having isolatedModules set to true.

🔎 Search Terms

  • isolatedModules
  • import type

🕗 Version & Regression Information

  • This is the behaviour in every recent version I tried.

⏯ Playground Link

See attached Zip Code and Readme.md for instructions.

💻 Code

The types.ts files contains only named exports

// types.ts

export interface IGEvent {
	data: {
		eventData: IGEventOptions;
	};
}

The index.ts imports the event interface and uses it within it's decorated method signature

// index.ts
import {Component, EventListener, GondelBaseComponent} from '@gondel/core';
import { IGEvent } from './types';

@Component('HelloWorld')
class HelloWorld extends GondelBaseComponent {

	@EventListener('gEvent')
	_handleEvent(event: IGEvent) {
		alert(event.data.eventData.helloWorld);
	}
}

The used library gondel is a component framework to start components on a page and handle their lifecycle.

The warning goes away when

  • we remove the decorator
  • we define event as any and cast it inside the method to the type IGEvent
  • we import it with import type { IGEvent } from ...

Code Example.zip

🙁 Actual behavior

When using Webpack, it throughs a build-breaking error since Webpack 5, before, a warning was shown.
When just using native TS for Compiling, I don't get an Error.

🙂 Expected behavior

Courtesy of explanation goes to @andrewbranch, see Ticket #40420.

The point of isolatedModules is to tell you ahead of time if a transpiler (like Babel or ts-loader in transpile mode) will not have enough information to know whether the code it produces at runtime is safe or not. That’s what’s happening here—ts-loader in transpile mode cannot possibly know whether IGEvent has a value meaning by only looking at index.ts, and so it stays in. Webpack is smart enough to detect this and tell you about it, and prior to v5, forgiving enough to fix up your runtime code. But at the core, what happened was you wrote something that could not be analyzed by ts-loader, and consequently ts-loader produced fundamentally broken output. isolatedModules is the way you tell the compiler that you’re going to use a transpiler, and so you want errors that prevent you from getting in this situation.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions