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

importsNotUsedAsValues in JS files elides imports incorrectly #38412

Closed
alan-agius4 opened this issue May 8, 2020 · 4 comments · Fixed by #38999
Closed

importsNotUsedAsValues in JS files elides imports incorrectly #38412

alan-agius4 opened this issue May 8, 2020 · 4 comments · Fixed by #38999
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@alan-agius4
Copy link
Contributor

alan-agius4 commented May 8, 2020

TypeScript Version: 4.0.0-dev.20200508

Search Terms:
importsNotUsedAsValues
import eliding

Code

In TypeScript 3.8, a new importsNotUsedAsValues feature/compilerOption was added. This, however is producing different output based on if the file is .ts or .js.

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "noResolve": true,
    "importsNotUsedAsValues": "preserve",
    "target": "ES2015",
    "outDir": "./out",
    "types": []
  }
}

test.js

// @ts-nocheck
import * as moment from 'moment';
import rollupMoment__default from 'moment';
export const moment = rollupMoment__default || moment;

Output

// @ts-nocheck
import * as moment from 'moment';
import 'moment';
export const moment = rollupMoment__default || moment;

Removing the importsNotUsedAsValues option, which is equivalent of having the value of remove will produce:

// @ts-nocheck
import * as moment from 'moment';
export const moment = rollupMoment__default || moment;

Changing the file extension from .js to .ts will produce the correct output for both cases

// @ts-nocheck
import * as moment from 'moment';
import rollupMoment__default from 'moment';
export const moment = rollupMoment__default || moment;

Expected behavior:

  • Used imports are not elided.
  • Given the same input it should produce the same output, irrespective of the file extension.

Actual behavior:

  • Given a JS input the import is elided incorrectly.

Playground Link: Playground doesn't have the importsNotUsedAsValues options.
Related Issues: None that I can find.

@alan-agius4
Copy link
Contributor Author

Note: this bug is also present in 3.8 & 3.9.

clydin pushed a commit to angular/angular-cli that referenced this issue May 11, 2020
With this change we "vendor" TypeScript 3.6 in build optimizer instead of using directly the npm package.

Reasons:
- We cannot update to a more recent version of TypeScript due to microsoft/TypeScript#38412
- Yarn workspace are not supported under Bazel. This means currently we are running unit testswhich uses a different TypeScript version at runtime.
- In TypeScript 3.9, the shape of ES2015 classes changed microsoft/TypeScript#32011, this requires some changes in the UT expects, but this again will not be correct to change now.
@andrewbranch andrewbranch added Bug A bug in TypeScript Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Jun 9, 2020
mhevery pushed a commit to angular/angular that referenced this issue Jun 11, 2020
Build-optimizer currently uses TypeScript 3.6 which is unable to resolve an 'accessor' in 'getTypeOfVariableOrParameterOrPropertyWorker'.

Unfortunately, in Build optimizer we cannot update the version of TypeScript because of microsoft/TypeScript#38412

PR Close #37456
mhevery pushed a commit to angular/angular that referenced this issue Jun 11, 2020
Build-optimizer currently uses TypeScript 3.6 which is unable to resolve an 'accessor' in 'getTypeOfVariableOrParameterOrPropertyWorker'.

Unfortunately, in Build optimizer we cannot update the version of TypeScript because of microsoft/TypeScript#38412

PR Close #37456
ngwattcos pushed a commit to ngwattcos/angular that referenced this issue Jun 25, 2020
…lar#37456)

Build-optimizer currently uses TypeScript 3.6 which is unable to resolve an 'accessor' in 'getTypeOfVariableOrParameterOrPropertyWorker'.

Unfortunately, in Build optimizer we cannot update the version of TypeScript because of microsoft/TypeScript#38412

PR Close angular#37456
profanis pushed a commit to profanis/angular that referenced this issue Sep 5, 2020
…lar#37456)

Build-optimizer currently uses TypeScript 3.6 which is unable to resolve an 'accessor' in 'getTypeOfVariableOrParameterOrPropertyWorker'.

Unfortunately, in Build optimizer we cannot update the version of TypeScript because of microsoft/TypeScript#38412

PR Close angular#37456
@milahu
Copy link

milahu commented Apr 15, 2021

Playground Link: Playground doesn't have the importsNotUsedAsValues options.

https://codesandbox.io/s/typescript-importsnotusedasvalues-preserve-bug-demo-qm23x

@andrewbranch
Copy link
Member

@milahu would you mind opening a new issue?

@milahu
Copy link

milahu commented Apr 15, 2021

#43693 (sorry for the noise)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants