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

Add isolatedModules error for ambiguous imports referenced in decorator metadata #42915

Merged
merged 8 commits into from
Mar 17, 2022

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Feb 22, 2021

Fixes #42281

Teeeechnically maybe a breaking change since there’s a new error, but it should only be an error when a transpiler would produce code that errors at runtime.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Feb 22, 2021
@andrewbranch andrewbranch assigned rbuckton and unassigned andrewbranch Apr 5, 2021
@rbuckton
Copy link
Member

If I understand correctly, the issue is that we keep in a static import for the interface:

import { X, B } from "./other";

class C extends B {
  @dec x: X;
}

We try to emit X as a value using a "fallback" expression:

import { X, B } from "./other";
class C extends B {
}
__decorate([
    dec,
    __metadata("design:type", typeof (_a = typeof X !== "undefined" && X) === "function" ? _a : Object)
], C.prototype, "x", void 0);

However, that leaves in a static import for X, which may not actually be exported from "./other". What we should do is replace the static import binding with a namespace import:

import { B } from "./other";
import * as other_1 from "./other"; // replace `import { X }` with namespace import...
class C extends B {
}
__decorate([
    dec,
    __metadata("design:type", typeof (_a = typeof other_1.X !== "undefined" && other_1.X) === "function" ? _a : Object)
], C.prototype, "x", void 0);

@rbuckton
Copy link
Member

This doesn't occur in CJS, AMD, or System emit because we always end up with a "namespace" import in those cases:

const other_1 = require("./other");
class C {
}
__decorate([
    dec,
    __metadata("design:type", typeof (_a = typeof other_1.X !== "undefined" && other_1.X) === "function" ? _a : Object)
], C.prototype, "x", void 0);

src/compiler/checker.ts Outdated Show resolved Hide resolved
@@ -879,6 +879,10 @@
"category": "Error",
"code": 1266
},
"A type referenced in a decorated signature must be imported with 'import type' when 'isolatedModules' and 'emitDecoratorMetadata' are enabled.": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider adding an import type quick-fix for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add if we decide to keep this approach after discussing in a design meeting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, along with a fix to convert named imports a namespace import, if it makes sense.

@andrewbranch
Copy link
Member Author

I’m not sure where the design meeting notes are where we discussed this, but I believe the compromise we came up with was

  • only error in es2015+ (done)
  • add codefixes that suggest both import type and import * as whatever as workarounds

@andrewbranch
Copy link
Member Author

I think to be safe, this shouldn’t go in post-beta, so will plan to target for 4.5.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I think now is a good time to put it into 4.7 too.

src/compiler/checker.ts Outdated Show resolved Hide resolved
src/compiler/checker.ts Outdated Show resolved Hide resolved
@@ -7111,6 +7115,7 @@
"code": 95166
},


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line seems extraneous

@andrewbranch andrewbranch merged commit df1faa0 into microsoft:main Mar 17, 2022
@andrewbranch andrewbranch added the Breaking Change Would introduce errors in existing code label Mar 17, 2022
@andrewbranch andrewbranch deleted the bug/42890 branch March 17, 2022 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Breaking Change Would introduce errors in existing code For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Missing --isolatedModules error for type referenced by decorator metadata
4 participants