Skip to content

Emit fallback for decorator metadata for type only imports #39337

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

Merged
merged 1 commit into from
Jul 2, 2020
Merged

Conversation

rbuckton
Copy link
Contributor

This fixes our emit for decorator metadata when the referenced type comes from a type-only import.

Fixes #37672

}
}

// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
if (!typeSymbol) {
return TypeReferenceSerializationKind.Unknown;
return isTypeOnly ? TypeReferenceSerializationKind.ObjectType : TypeReferenceSerializationKind.Unknown;
Copy link
Member

Choose a reason for hiding this comment

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

How do we know the type is objecty? You can import type a primitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeSerializationKind.ObjectType tells the ts transform to emit Object for the type metadata (which essentially means any). TypeSerializationKind.Unknown means we should try to resolve a value at runtime using that name (by using typeof testing), which would be incorrect behavior. Consider the following using the current version of the compiler (without this change):

// ts
import { dec } from "./decorators";
import type { Map } from "./myMap";

@dec
export class C {
  constructor(map: Map) {}
}

// js
...
let C = /** @class */ (() => {
    var _a;
    let C = class C {
        constructor(map) { }
    };
    C = __decorate([
        dec,
        __metadata("design:paramtypes", [typeof (_a = typeof Map !== "undefined" && Map) === "function" ? _a : Object])
    ], C);
    return C;
})();

The design:paramtypes metadata for C will incorrectly use the global Map object.

For a type-only import we can be certain that the type cannot be referenced as a value, so we should emit Object instead of trying to resolve a value for it.

@rbuckton rbuckton merged commit 652a1c5 into master Jul 2, 2020
@rbuckton rbuckton deleted the fix37672 branch July 2, 2020 18:39
@liorcode
Copy link

Seems this problem still happens in typescript 4.1.3, when using a type which is nested inside a namespace.

For example:

// services.ts

export namespace Services {
  export class Service {}
}


// main.ts
import type { Services } from './services';

declare const decorator: any;
export class Main {
  @decorator()
  fieldTest: Services.Service;
}

JS output:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Main = void 0;
const tslib_1 = require("tslib");
class Main {
}
tslib_1.__decorate([
    decorator(),
    tslib_1.__metadata("design:type", Services.Service)
], Main.prototype, "fieldTest", void 0);
exports.Main = Main;

(Services is undefined)

@andrewbranch
Copy link
Member

@liorcode would you mind filing a new issue? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

emitDecoratorMetadata causes runtime errors by referencing type-only imports
4 participants