tsconfig-paths icon indicating copy to clipboard operation
tsconfig-paths copied to clipboard

TypeScript "importsNotUsedAsValues" compiler option doesn't take effect while using path alias

Open miZyind opened this issue 4 years ago • 2 comments

I'm submitting a...

[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

Here are the typescript settings and the minimum sample code:

// tsconfig.json
{
  "include": ["src", "typings"],
  "exclude": ["dist", "node_modules"],
  "compilerOptions": {
    // Avoid to emit unused imports for side effects (this is the default option even if I didn't set it up)
    "importsNotUsedAsValues": "remove",
    "isolatedModules": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "#configs/*": ["src/configs/*"],
    },
    // ...
  }
}
// src/configs/app.ts

export = {
  name: 'Example App',
  // ...
};
// src/configs/index.ts

import type App from '#configs/app'; // TypeScript 3.8 "Type-Only Imports and Exports" syntax

type AppConfig = typeof App;

export type { AppConfig };

I'm using NestJS, so build the project through NestJS-CLI with the command: nest build

The output file:

// dist/configs/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Shouldn't emit the following 2 lines since I imported the "Type" only
const tslib_1 = require("tslib");
const app_1 = tslib_1.__importDefault(require("./app"));

The output would be correct if I only modify src/configs/index.ts to the following code:

// src/configs/index.ts

import type App from './app'; // import directly without using "Path Alias"

type AppConfig = typeof App;

export type { AppConfig };

Build the project again through nest build

The output file:

// dist/configs/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

Expected behavior

The following code:

// src/configs/index.ts

import type App from '#configs/app';

type AppConfig = typeof App;

export type { AppConfig };

should emit the following result:

// dist/configs/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

Minimal reproduction of the problem with instructions

The same as the description of Current behavior

What is the motivation / use case for changing the behavior?

Path Alias function should emit the same code as the output of the original tsc

Environment

[System Information]
OS Version     : macOS Catalina
NodeJS Version : v14.2.0
YARN Version    : 1.22.4 

[Nest CLI]
Nest CLI Version : 7.4.1 

[Nest Platform Information]
platform-express version      : 7.3.1
common version                : 7.3.1
config version                : 0.5.0
core version                  : 7.3.1
tsconfig-paths version        : 3.9.0
tsconfig-paths-webpack-plugin : 3.2.0

Related

https://github.com/nestjs/nest-cli/issues/783

miZyind avatar Jul 10 '20 06:07 miZyind

@miZyind Looks like a problem with nest. It's not clear how tsconfig-paths can possibly be causing this bug, since it's only responsible for resolving paths, not for stripping type-only imports from the output.

Did they give you any indication why they believe this is a tsconfig-paths bug, and not a nest bug?

cspotcode avatar Jul 26 '20 17:07 cspotcode

@cspotcode Thanks for your reply. They didn't provide me the reason why this issue related to tsconfig-paths instead of nest itself. I'll try to contact them to figure out if there is anything I can help or extra information I need to provide.

miZyind avatar Jul 27 '20 02:07 miZyind