-
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
jsdoc @enum with import/export #33575
Comments
I have the inverse of this issue, where I can't do something like: import {MyEnum} from './my-enum.js';
const foo = MyEnum.a; // "'MyEnum' only refers to a type, but is being used as a value here"
const bar = MyEnum['b']; // Same thing
switch(baz) {
case MyEnum.a: // and also here
console.log("xyz");
break;
case MyEnum.b: //and here
break;
default:
} It used to work as of TypeScript 3.5.3, I discovered this issue when upgrading a project this morning. |
I can confirm that 3.5.3 was the last version where @art-in's scenario worked. We are having the same issue in OpenLayers. What still works is this: // main.js
import MyEnum from './my-enum.js';
/** @type {import("./my-enum.js").default} */ // No error
const v = MyEnum.b; But that's a bit cumbersome, considering that we have MyEnum imported already. |
Given that 3.7 has the game-changing #7546, I would be very sad to not be able to use it because of this. |
#34515 appears to improve the situation -- it resolves a type -- but it resolves the wrong type. The problem is that the imported I think the fix is to resolve the alias symbol for |
I believe @elibarzilay is looking at the alias resolution code right now in order to fix the symbol's flags. I hope it's an overlooked code path and not something deep in alias resolution. |
I just tried v3.8.1-rc, but this is still broken. Has there been any progress? |
Thanks everyone for the great work recently on JSDoc support, which allows us to publish auto-generated .d.ts files from our JSDoc annotated JavaScript code base. That's awesome! This one is the last blocker for TypeScript being 100% useful with JSDoc annotations. Is there a new milestone for this? It is still broken in v3.9.0-dev. |
The problem boils down to this: Assume you have the following /** @enum {number} */
const MyEnum = {
a: 1,
b: 2
};
export default MyEnum; Now when you import the enum in a different file, the following will work: import MyEnum from './my-enum.js';
/** @type {import("./my-enum.js").default} */
const v = MyEnum.b; The follwoing will cause the reported error: import MyEnum from './my-enum.js';
/** @type {MyEnum} */
const v = MyEnum.b; I believe this is because import MyEnum from './my-enum.js';
/** @type {typeof MyEnum} */
const v = MyEnum.b; |
Resummarizing this bug, you cannot import a value marked with @enum. This regressed in TypeScript 3.6.
workaround is to write |
Quick note: the |
And what @elibarzilay mentions also happens when the workaround mentioned in #33575 (comment) is used. |
Nameless jsdoc typedefs have their exportedness controlled by the exportedness of the location they pull their name from. Fixes microsoft#33575.
Nameless jsdoc typedefs have their exportedness controlled by the exportedness of the location they pull their name from. Fixes microsoft#33575.
Quick note²: The |
Nameless jsdoc typedefs have their exportedness controlled by the exportedness of the location they pull their name from. Fixes #33575.
TypeScript Version: 3.6.3
Code
This works:
This doesn't work (
MyEnum
imported from separate file):Expected behavior:
No type errors.
It did work fine in v2.8.3
The text was updated successfully, but these errors were encountered: