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

compilation doesn't omit import that is not used in runtime if typescript alias is created from imported type #9368

Closed
layerok opened this issue Aug 1, 2024 · 2 comments · Fixed by #9369
Assignees
Labels
Milestone

Comments

@layerok
Copy link

layerok commented Aug 1, 2024

Describe the bug

compilation doesn't omit import statement that is not used in runtime if typescript alias is created from imported type

Input code

import {Schemas} from './types.d';
import AddressResource = Schemas.AddressResource;

Config

{}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.4&code=H4sIAAAAAAAAA8vMLcgvKlGoDk7OSM1NLK5VSCvKz1VQ19MvqSxILdZLUbfm5cqEqHFMSSlKLS4OSi3OLy1KTlWwVYBq0kOTsQYAWJI5D1UAAAA%3D&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3CHK0KF36CEQdSIqfsKOVBTl7oUE0maz38fveRVSwps0PORaxrJElQjTuReEsmf1KQhwjkg6mchw6yxTpSZlCXdoOxhglWbk6kIah3FoDrAhEHZHw5zxZsr%2FmTq4mJDoKqxS5WeL10TRUsGF17KT7Zfa92hwh5%2Boh52HwdCzOzktKLYvIO%2B2AhcBAAA%3D

SWC Info output

npx -y swc-info@latest
    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:52 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8103
        CPU: (8 cores)
            Models: Apple M1

    Binaries:
        Node: 20.0.0
        npm: 9.6.4
        Yarn: 1.22.19
        pnpm: 9.4.0

    Relevant Packages:
        @swc/core: 1.3.89
        @swc/helpers: N/A
        @swc/types: 0.1.12
        

    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A
        

Expected behavior

import statement that is unsused in runtime is omitted

Actual behavior

import statement that is unsused in runtime is preserved

Version

1.7.4

Additional context

I'm using swc in my Vite project. After updating swc to version 1.3.89 I started seeing this error in the browser console.

SyntaxError: The requested module './types.d.ts' does not provide an export named 'Schemas'

swc prior to version 1.3.89 omitted import that was unused in runtime. That is why there was no such error in the browser console before.

Also I noticed if I use import type then swc of 1.3.89 version works as expected.

import type {Schemas} from './types.d';
import AddressResource = Schemas.AddressResource;

swc output is

export {};

I guess this issue occured after issue #7177 was fixed.

@layerok layerok added the C-bug label Aug 1, 2024
@magic-akari
Copy link
Member

magic-akari commented Aug 1, 2024

As a workaround, use type import

import type { Schemas } from './types.d';
import type AddressResource = Schemas.AddressResource;

@magic-akari magic-akari self-assigned this Aug 1, 2024
@kdy1 kdy1 added this to the Planned milestone Aug 2, 2024
@kdy1 kdy1 closed this as completed in #9369 Aug 2, 2024
@kdy1 kdy1 closed this as completed in 4f9116f Aug 2, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.7.5 Aug 2, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Sep 1, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants