-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow inlined-value usage of type-only const enums #46556
Conversation
The TypeScript team hasn't accepted the linked issue #40344. If you can get it accepted, this PR will have a better chance of being reviewed. |
If BeforeisolatedModules false (same as direction $ tsc value-usage-of-type-only-const-enum.ts
value-usage-of-type-only-const-enum.ts:3:19 - error TS1361: 'Direction' cannot be used as a value because it was imported using 'import type'.
3 const direction = Direction.Up;
~~~~~~~~~
value-usage-of-type-only-const-enum.ts:1:15
1 import type { Direction } from "./direction";
~~~~~~~~~
'Direction' was imported here.
Found 1 error. isolatedModules true (same as false): $ tsc --isolatedModules value-usage-of-type-only-const-enum.ts
value-usage-of-type-only-const-enum.ts:3:19 - error TS1361: 'Direction' cannot be used as a value because it was imported using 'import type'.
3 const direction = Direction.Up;
~~~~~~~~~
value-usage-of-type-only-const-enum.ts:1:15
1 import type { Direction } from "./direction";
~~~~~~~~~
'Direction' was imported here.
Found 1 error. AfterisolatedModules false (error no more): $ tsc value-usage-of-type-only-const-enum.ts isolatedModules true (same as before): $ tsc --isolatedModules value-usage-of-type-only-const-enum.ts
value-usage-of-type-only-const-enum.ts:3:19 - error TS1361: 'Direction' cannot be used as a value because it was imported using 'import type'.
3 const direction = Direction.Up;
~~~~~~~~~
value-usage-of-type-only-const-enum.ts:1:15
1 import type { Direction } from "./direction";
~~~~~~~~~
'Direction' was imported here.
Found 1 error. |
@andrewbranch this PR is pretty small. From the issue discussion, it sounds like you'd be interested in a fix if it was simple. What do you think about this? |
We’ve always known this change would be dead simple, but the issue is “Awaiting More Feedback.” The fairly low amount of feedback we’ve received on it has been positive, and I’ve been cautiously open to this change from the beginning. I think the downside is just adding to the confusion/complexity around what |
I'm still stuck on, you e.g.
On the other hand I'm cool waiting for more feedback 👍, in case a better solution can be found, or you can sidestep it entirely, or I'm missing the bigger picture, or there are developments on the horizon ...
Instead of "if isolatedModules is false" I should have said "if inlined", whether by |
I realized another way of putting this is making type-only and ambient const enums equivalent (without the same equivalence for non-const enums). Roughly, making type-only and |
To help with PR housekeeping, I'm going to close this PR while it's still waiting on its bug to be accepted. |
Allow, e.g.
if isolatedModules is false.
Motivation
eslint-plugin-import checks whether import statements are resolvable, so wants to know whether an import statement will be elided (by TypeScript) or not. Even if it could predict which imports were candidates for elision, that behavior is configurable (importsNotUsedAsValues), so the plugin takes the conservative view that all imports except type-only imports might or might not be elided.
This is a problem if type-only const enums can't be used as values:
export const enum Direction { Up, Down, Left, Right }
indirection.d.ts
import { Direction } from "./direction"
, eslint-plugin-import warns that the import might not be resolvable (at runtime, assuming there's nodirection.js
to matchdirection.d.ts
)The same problem as #40344.
I understand that type-only means type-only, and a type-only import on the right-hand side of an assignment looks really unexpected: #36003 (comment)
On the other hand going back to the motivation for type-only imports, my impression is that the whole deal was predictable import elision? #35200
If isolatedModules is false:
For these reasons, I'd like to allow value usage of type-only const enums if isolatedModules is false.
Before
isolatedModules false:
isolatedModules true:
After
isolatedModules false (error no more):
isolatedModules true (same as before):
Fixes #40344